A small express middleware for standardizing on errors, it includes a few error types and more are easily added
At i.TV, we tend to do error handling from the server lots of different ways, here is how we standardize on it
vare=require("std-error")returnnext(newe.NotFound())...app.use(function(err,req,resnext){res.json(err.statusCode,{error: true,message: err.message})})// or, which is the the same as above (works for express 2.0 and 3.0)app.use(e.defaultHandler)//express 3.0app.error(e.defaultHandler)// express 2.0// Register custom errorse.register({type: "MyError",message: "Something bad happened",statusCode: 9000})// Register custom error with a custom errorCodee.register({type: "MyErrorWithErrorCode",message: "Something bad happened",statusCode: 500,errorCode: 1000})returnnext(newe.MyError())// change default status codese.NotFound.prototype.statusCode=405{type: "BadParameter",message: "Invalid parameter in request",statusCode: 400},{type: "Unauthorized",message: "Missing or Invalid credentials",statusCode: 401},{type: "NotFound",message: "Resource not found",statusCode: 404},{type: "InvalidMethod",message: "Invalid method requested",statusCode: 405},{type: "Timeout",message: "Request Timeout",statusCode: 408},{type: "Error",message: "Unexpected Error Occured",statusCode: 500},{type: "ServerError",message: "Unexpected Error Occured",statusCode: 500},{type: "DatabaseError",message: "Unexpected Error Occured",statusCode: 500},{type: "RequestError",message: "Unexpected Error Occured",statusCode: 500}