Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork69
Development-only error handler middleware
License
expressjs/errorhandler
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Development-only error handler middleware.
This middleware is only intended to be used in a development environment, asthefull error stack traces and internal details of any object passed to thismodule will be sent back to the client when an error occurs.
When an object is provided to Express as an error, this module will displayas much about this object as possible, and will do so by using content negotiationfor the response between HTML, JSON, and plain text.
- When the object is a standard
Errorobject, the string provided by thestackproperty will be returned in HTML/text responses. - When the object is a non-
Errorobject, the result ofutil.inspectwill be returned in HTML/text responses. - For JSON responses, the result will be an object with all enumerable propertiesfrom the object in the response.
This is aNode.js module available through thenpm registry. Installation is done using thenpm install command:
$ npm install errorhandler
varerrorhandler=require('errorhandler')
Create new middleware to handle errors and respond with content negotiation.
Error handler accepts these properties in the options object.
Provide a function to be called with the error and a string representation ofthe error. Can be used to write the error to any desired location, or set tofalse to only send the error back in the response. Called aslog(err, str, req, res) whereerr is theError object,str is a stringrepresentation of the error,req is the request object andres is theresponse object (note, this function is invokedafter the response has beenwritten).
The default value for this option istrue unlessprocess.env.NODE_ENV === 'test'.
Possible values:
true: Log errors usingconsole.error(str).false: Only send the error back in the response.- A function: pass the error to a function for handling.
Basic example of adding this middleware as the error handler only in developmentwithconnect (express also can be used in this example).
varconnect=require('connect')varerrorhandler=require('errorhandler')varapp=connect()// assumes NODE_ENV is set by the userif(process.env.NODE_ENV==='development'){// only use in developmentapp.use(errorhandler())}
Sometimes you may want to output the errors to a different location than STDERRduring development, like a system notification, for example.
varconnect=require('connect')varerrorhandler=require('errorhandler')varnotifier=require('node-notifier')varapp=connect()// assumes NODE_ENV is set by the userif(process.env.NODE_ENV==='development'){// only use in developmentapp.use(errorhandler({log:errorNotification}))}functionerrorNotification(err,str,req){vartitle='Error in '+req.method+' '+req.urlnotifier.notify({title:title,message:str})}
About
Development-only error handler middleware
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors15
Uh oh!
There was an error while loading.Please reload this page.