Skip to content

Commit

Permalink
changed all exception throwing to callback error passing
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrcurtis committed Jun 11, 2012
1 parent 5597381 commit afaa1ee
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Expand Up @@ -59,4 +59,9 @@ Initial release
0.1.2 - 27-05-2012
------------------

1. Unsupported files should not crash
1. Unsupported files should not crash

0.1.2 - 27-05-2012
------------------

1. refactored to pass all errors to callback instead of throwing since nodejs does not pass thrown exceptions up the call stack as expected
70 changes: 34 additions & 36 deletions easyimage.js
Expand Up @@ -10,11 +10,9 @@ var error_messages = {
'unsupported': 'File not supported.',
};

// function to throw errors at unsuspecting and potentially innocent developers
var throw_err = function(type) { throw(error_messages[type]); };

// general info function
function info(file, callback) {
if (callback === undefined)return;
file = quoted_name(file);
// %z = depth, %m = type, %w = width, %h = height, %b = filesize in byte, %f = filename
imcmd = 'identify -format "%m %z %w %h %b %f" ' + file;
Expand All @@ -23,23 +21,23 @@ function info(file, callback) {
var info = {};
//Basic error handling
if (stderr.match(/^identify:/)) {
callback(error_messages['unsupported'], stdout, stderr);
} else {
var temp = stdout.split(' ');
//Basic error handling:
if (temp.length < 6) {
callback(error_messages['unsupported'], stdout, stderr);
} else {
info.type = temp[0];
info.depth = temp[1];
info.width = temp[2];
info.height = temp[3];
info.size = temp[4];
info.name = temp.slice(5).join(' ').replace(/(\r\n|\n|\r)/gm,'');
callback(err, info, stderr);
}
}
return callback(error_messages['unsupported'], stdout, stderr);
} else {
var temp = stdout.split(' ');
//Basic error handling:
if (temp.length < 6) {
return callback(error_messages['unsupported'], stdout, stderr);
} else {
info.type = temp[0];
info.depth = temp[1];
info.width = temp[2];
info.height = temp[3];
info.size = temp[4];
info.name = temp.slice(5).join(' ').replace(/(\r\n|\n|\r)/gm,'');
return callback(err, info, stderr);
}
}
});
}

Expand All @@ -58,36 +56,36 @@ exports.info = function(file, callback) {

// convert a file type to another
exports.convert = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
options.src = quoted_name(options.src);
options.dst = quoted_name(options.dst);
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) throw err;
if (err)return callback(err);
info(options.dst, callback);
});
};

// resize an image
exports.resize = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.width === undefined) throw_err('dim');
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(error_messages['dim']);
options.height = options.height || options.width;
options.src = quoted_name(options.src);
options.dst = quoted_name(options.dst);
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) throw err;
if (err)return callback(err);
info(options.dst, callback);
});
};

// crop an image
exports.crop = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.cropwidth === undefined) throw_err('dim');
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.cropwidth === undefined)return callback(error_messages['dim']);
options.cropheight = options.cropheight || options.cropwidth;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
Expand All @@ -98,16 +96,16 @@ exports.crop = function(options, callback) {
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) throw err;
if (err)return callback(err);
info(options.dst, callback);
});

};

// resize and crop in one shot!
exports.rescrop = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.width === undefined) throw_err('dim');
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(error_messages['dim']);
options.height = options.height || options.width;

options.cropwidth = options.cropwidth || options.width;
Expand All @@ -122,16 +120,16 @@ exports.rescrop = function(options, callback) {
if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -resize ' + options.width + 'x' + options.height + options.fill + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize ' + options.width + 'x' + options.height + options.fill + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
if (err) throw err;
if (err)return callback(err);
info(options.dst, callback);
});
};

// create thumbnails
exports.thumbnail = function(options, callback) {

if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.width === undefined) throw_err('dim');
if (options.src === undefined || options.dst === undefined)return callback(error_messages['path']);
if (options.width === undefined)return callback(error_messages['dim']);
options.height = options.height || options.width;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
Expand All @@ -140,7 +138,7 @@ exports.thumbnail = function(options, callback) {
options.dst = quoted_name(options.dst);

info(options.src, function(err, original, stderr) {
if (err) throw err;
if (err)return callback(err);

// dimensions come as strings, convert them to number
original.width = +original.width;
Expand All @@ -157,7 +155,7 @@ exports.thumbnail = function(options, callback) {
else imcmd = 'convert ' + options.src + ' -resize '+ resizewidth + 'x' + resizeheight + ' -quality ' + options.quality + ' -gravity ' + options.gravity + ' -crop '+ options.width + 'x'+ options.height + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;

child = exec(imcmd, function(err, stdout, stderr) {
if (err) throw err;
if (err)return callback(err);
info(options.dst, callback);
});

Expand All @@ -168,7 +166,7 @@ exports.thumbnail = function(options, callback) {
exports.exec = function(command, callback) {
var _command = command.split(' ')[0];
// as a security measure, we will allow only 'convert' commands
if (_command != 'convert') throw_error('restricted');
if (_command != 'convert')return callback(error_messages['restricted']);

child = exec(command, function(err, stdout, stderr) { callback(err, stdout, stderr); });
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "easyimage",
"version": "0.1.2",
"version": "0.1.3",
"description": "A user-friendly module for processing images in Node.js.",
"main": "easyimage.js",
"author": "Hack Sparrow <captain@hacksparrow.com>",
Expand Down

0 comments on commit afaa1ee

Please sign in to comment.