Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 Commits

Repository files navigation

http-errors

This is a collection of subclassed JavaScript Error objects, so it can be used whenever a plain Error object is used. More specifically, pass an instance of this error to the app middleware whenever a HTTP error response needs to be surfaced.

In addition to the default Error.name and Error.message properties, it comes with:

  • Error.code: The HTTP status code of the error response
  • Error.fields: Experimental This attaches the bad fields in REST API responses. Defaults to {}.

When using promises, you can throw these errors directly to have the error handled in the subsequent then/catch/fail handler.

Usage:

// Some page controller middlewarevarHttpErrors=require('http-errors');module.exports=function(req,res,next){api.findEventById(req.params.id,function(err,event){// Next error middleware will output the error page with HTTP 404if(err){returnnext(newHttpErrors.ResourceNotFound('Event not found'));}res.render(...);});}// Error middlewarefunction(req,res,next,err){res.render('error',{title: 'Error Page',status: err.code,// This is how you use `Error.code`message: err.message// Or the error message if you want...});}

Original inspiration (and ideas for improvements) from:

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages