Permalink
Browse files

using emitter instead of throwing errors since callbacks might throw..

use .on('error'...) to catch errors
  • Loading branch information...
1 parent 3cfe80d commit 7d0779ab199a3387537d8355d0ce24882e252f85 @saml saml committed Nov 23, 2010
Showing with 32 additions and 36 deletions.
  1. +32 −36 src/main.js
View
@@ -3,6 +3,7 @@ var fs = require('fs');
var sys = require('sys');
var path = require('path');
var child = require('child_process');
+var events = require('events');
var util = require('./util');
var tempfile = require('./tempfile');
@@ -16,6 +17,7 @@ var settings = require('./settings');
var Server = function(convert, srcDir, destDir) {
var me = {};
+
var regex = /^\/(.+)\.c\.(\d+)x(\d+)(\.\w+)$/;
var parseUrl = function(url) {
@@ -55,6 +57,9 @@ var Server = function(convert, srcDir, destDir) {
var Handler = function(request, response) {
var me = {};
+ var emitter = new events.EventEmitter();
+ me.emitter = emitter;
+
var serveFile = function(filePath, mimeType, callback) {
if (!mimeType) {
mimeType = getMimeType(filePath);
@@ -64,7 +69,7 @@ var Server = function(convert, srcDir, destDir) {
fs.stat(filePath, function(err, stat) {
if (err || !stat.isFile()) {
- me.do404();
+ emitter.emit('error', me.do404, 'not found: ' + filePath);
return;
}
@@ -86,7 +91,7 @@ var Server = function(convert, srcDir, destDir) {
}
});
stream.on('error', function(err) {
- throw new Error('error reading file: ' + filePath);
+ emitter.emit('error', me.do500, 'error reading file: ' + filePath);
});
});
};
@@ -104,18 +109,17 @@ var Server = function(convert, srcDir, destDir) {
callback(out);
}
} else {
- throw new Error(
- 'convert %s exited with %s'.f(args.join(' '), code));
+ emitter.emit('error', me.do500, 'convert %s exited with %s'.f(args.join(' '), code));
}
});
};
util.mkdirP(basePath, 0755, function(err) {
if (err) {
sys.log(err);
- throw new Error('cannot create directory: ' + basePath);
+ emitter.emit('error', me.do500, 'cannot create directory: ' + basePath);
} else {
- doResize();
+ process.nextTick(doResize);
}
});
@@ -127,7 +131,7 @@ var Server = function(convert, srcDir, destDir) {
});
var data = 'Not Found: ' + request.url;
if (msg) {
- data = '\n%s'.f(data);
+ data = '%s\n%s'.f(data, msg);
}
response.end(data);
};
@@ -138,7 +142,7 @@ var Server = function(convert, srcDir, destDir) {
});
var data = 'Internal Error: ' + request.url;
if (msg) {
- data = '\n%s'.f(msg);
+ data = '%s\n%s'.f(data, msg);
}
response.end(data);
};
@@ -149,21 +153,21 @@ var Server = function(convert, srcDir, destDir) {
});
var data = 'Not Supported Method: %s %s'.f(request.method, request.url);
if (msg) {
- data = '\n%s'.f(msg);
+ data = '%s\n%s'.f(data, msg);
}
response.end(data);
};
- me.start = function() {
+
+ var start = function() {
if (request.method !== 'GET') {
- me.do501();
+ emitter.emit('error', me.do501);
return;
}
var o = parseUrl(request.url);
if (!o) {
- sys.log('cannot parse: ' + request.url);
- me.do404();
+ emitter.emit('error', me.do404, 'cannot parse: ' + request.url);
return;
}
@@ -177,37 +181,27 @@ var Server = function(convert, srcDir, destDir) {
});
});
});
- /*
- path.exists(o.out, function(exists) {
- if (exists) {
- sys.log('serving from cache: ' + o.out);
- serveFile(o.out);
- } else {
- sys.log('creating cache: ' + o.out);
- var size = '%sx%s'.f(o.width, o.height);
- resizeImage(o.src, o.out, size, function() {
- serveFile(o.out);
- });
-
- }
- });
- */
};
+ me.start = function() {
+ process.nextTick(start);
+ }
+
+
return me;
};
me.server = http.createServer(function(request, response) {
var handler = Handler(request, response);
- try {
- handler.start();
- } catch (e) {
- var msg = sys.inspect(e, true, null);
- sys.log('error while handling %s %s'.f(request.url, e));
- handler.do500();
- }
+ handler.emitter.on('error', function(f, msg) {
+ if (typeof f === 'function') {
+ sys.log(msg);
+ f();
+ }
+ });
+ handler.start();
});
me.server.on('close', function() {
@@ -279,7 +273,9 @@ var main = function() {
process.on('SIGTERM', exit);
process.on('SIGQUIT', exit);
process.on('SIGKILL', exit);
-
+ //process.on('uncaughtException', function(err) {
+ // sys.log(err);
+ //});
server.start(host, port);
};

0 comments on commit 7d0779a

Please sign in to comment.