Permalink
Browse files

Custom errors, exporting errors for using with exedra

  • Loading branch information...
1 parent 81188b0 commit 51787cfba1fb80e0ff02459882b468771c538392 @corpix committed Dec 27, 2011
Showing with 41 additions and 33 deletions.
  1. +40 −32 index.js
  2. +1 −1 package.json
View
@@ -1,46 +1,54 @@
-module.exports = function(app, opts){
+var ex = {};
+
+function NotFound(){
+ this.name = 'NotFound';
+ this.status = 404;
+ this.message = 'Oops! The page you requested doesn\'t exist';
+
+ Error.call(this, this.message);
+ Error.captureStackTrace(this, arguments.callee);
+}
+
+NotFound.prototype.__proto__ = Error.prototype;
+
+ex.NotFound = NotFound;
+
+ex.bind = function(app, opts){
app.use(function(req, res, next){
- next(new NotFound(req.url))
- })
-
- function NotFound(path){
- this.name = 'NotFound'
- if (path) {
- Error.call(this, 'Cannot find ' + path)
- this.path = path
- } else {
- Error.call(this, 'Not Found')
- }
- Error.captureStackTrace(this, arguments.callee)
- }
-
- NotFound.prototype.__proto__ = Error.prototype
+ next(new NotFound());
+ });
app.error(function(err, req, res, next){
- if (err instanceof NotFound){
+ if(!err.name || err.name == 'Error' || !ex[err.name]){
+ console.log(err.stack);
if(req.xhr)
- return res.send({ error: 'Not found' }, 404)
+ return res.send({ error: 'Internal error' }, 500);
- res.render('errors/404', {
- layout: opts.layout,
- status: 404,
- error: err,
- showStack: app.settings.showStackError,
- title: 'Oops! The page you requested doesn\'t exist'
- })
- } else {
- console.log(err.stack)
- if(req.xhr)
- return res.send({ error: 'Internal error' }, 500)
- res.render('errors/500', {
+ return res.render('errors/500', {
layout: opts.layout,
status: 500,
error: err,
showStack: app.settings.showStackError,
title: 'Oops! Something went wrong!'
- })
+ });
}
- })
+
+ if(req.xhr)
+ return res.send({ error: err.name }, err.status);
+
+ res.render('errors/' + err.status, {
+ layout: opts.layout,
+ status: err.status,
+ error: err,
+ showStack: app.settings.showStackError,
+ title: err.message
+ });
+
+
+ });
}
+
+
+module.exports = ex;
View
@@ -2,7 +2,7 @@
"author": "Corpix <me@corpix.ru> (http://corpix.ru)",
"name": "express-errors",
"description": "Error handler",
- "version": "0.0.1",
+ "version": "0.0.2",
"repository": {
"type": "git",
"url": "git://github.com/corpix/express-errors.git"

0 comments on commit 51787cf

Please sign in to comment.