diff --git a/CHANGELOG.md b/CHANGELOG.md index e1f400f..2e57a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,4 +59,9 @@ Initial release 0.1.2 - 27-05-2012 ------------------ -1. Unsupported files should not crash \ No newline at end of file +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 \ No newline at end of file diff --git a/easyimage.js b/easyimage.js index e468d2e..db7defb 100644 --- a/easyimage.js +++ b/easyimage.js @@ -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; @@ -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); + } + } }); } @@ -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; @@ -98,7 +96,7 @@ 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); }); @@ -106,8 +104,8 @@ exports.crop = function(options, 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; @@ -122,7 +120,7 @@ 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); }); }; @@ -130,8 +128,8 @@ exports.rescrop = function(options, 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; @@ -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; @@ -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); }); @@ -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); }); }; diff --git a/package.json b/package.json index 3b5a937..b2ed89c 100644 --- a/package.json +++ b/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 ",