Permalink
Browse files

Merge branch 'development' of github.com:remy/jsbin into development

  • Loading branch information...
2 parents 9e637e7 + bf76b8a commit dd59494e655d1cc34f2ab5f0db813c07b280d91a @remy remy committed Jul 23, 2012
Showing with 47 additions and 28 deletions.
  1. +6 −0 lib/errors.js
  2. +1 −1 lib/handlers/bin.js
  3. +40 −27 lib/handlers/error.js
View
@@ -49,6 +49,12 @@ exports.NotFound = HTTPError.extend({
}
});
+exports.BinNotFound = exports.NotFound.extend({
+ constructor: function BinNotFound(message) {
+ exports.NotFound.call(this, 404, message);
+ }
+});
+
// Creates an error object for handling 401 responses.
exports.NotAuthorized = HTTPError.extend({
constructor: function NotAuthorized(message) {
View
@@ -341,7 +341,7 @@ module.exports = Observable.extend({
}
if (!result) {
- return next(new errors.NotFound('Could not find bin: ' + req.params.bin));
+ return next(new errors.BinNotFound('Could not find bin: ' + req.params.bin));
} else {
req.bin = result;
// manually add the full url to the bin to allow templates access
View
@@ -21,37 +21,26 @@ module.exports = Observable.extend({
httpError: function (err, req, res, next) {
err = this.coerceError(err);
- if (err instanceof errors.NotFound) {
- return (new BinHandler(this.sandbox)).notFound(req, res);
- } else if (err instanceof errors.HTTPError) {
- res.statusCode = err.status;
-
- if (req.accepts(['html'])) {
- res.contentType('html');
- res.send(err.toHTMLString());
- } else if (req.accepts(['json'])) {
- res.json(err);
+ if (err instanceof errors.NotFound && req.accepts('html')) {
+ if (err instanceof errors.BinNotFound) {
+ return (new BinHandler(this.sandbox)).notFound(req, res);
} else {
- res.contentType('txt');
- res.send(err.toString());
+ return this.renderErrorPage(err, req, res);
}
-
- return;
+ } else if (err instanceof errors.HTTPError) {
+ return this.renderError(err, req, res);
}
+
next(err);
},
// Fall through handler for when no routes match.
notFound: function (req, res, next) {
if (req.accepts('html')) {
- res.status(404).render('error', {
- is_404: true,
- root: this.helpers.url(),
- dave: this.helpers.url('/images/logo.png', false, true)
- });
+ this.renderErrorPage(err, req, res);
} else {
var error = new errors.NotFound('Page Does Not Exist');
- this.httpError(error, req, res, next);
+ this.renderError(error, req, res);
}
},
@@ -61,16 +50,38 @@ module.exports = Observable.extend({
this.sendErrorReport(err, req);
if (req.accepts('html')) {
- res.status(500).render('error', {
- is_500: true,
- dave: this.helpers.url('/images/logo.png', false, true)
- });
+ this.renderErrorPage(err, req, res);
} else {
var error = new errors.HTTPError(500, 'Internal Server Error');
- this.httpError(error, req, res, next);
+ this.renderError(error, req, res);
+ }
+ },
+
+ renderError: function (err, req, res) {
+ res.status(err.status);
+
+ if (req.accepts(['html'])) {
+ res.contentType('html');
+ res.send(err.toHTMLString());
+ } else if (req.accepts(['json'])) {
+ res.json(err);
+ } else {
+ res.contentType('txt');
+ res.send(err.toString());
}
},
+ renderErrorPage: function (err, req, res) {
+ var status = err.status || 500;
+
+ res.status(status).render('error', {
+ is_500: status !== 404, // Saves us having many error pages at the mo.
+ is_404: status === 404,
+ root: this.helpers.url(),
+ dave: this.helpers.url('/images/logo.png', false, true)
+ });
+ },
+
sendErrorReport: function (err, req) {
var to = this.helpers.set('notify errors'),
session = utils.extend({}, req.session),
@@ -103,8 +114,10 @@ module.exports = Observable.extend({
// it into an instance of HTTPError. Just returns this original error
// if no status is found.
coerceError: function (err) {
- if (!(err instanceof errors.HTTPError) && err.status) {
- return errors.create(err.status, err.message);
+ var status = typeof err === 'number' ? err : err.status;
+
+ if (!(err instanceof errors.HTTPError) && status) {
+ return errors.create(status, err.message);
}
return err;
}

0 comments on commit dd59494

Please sign in to comment.