Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Development-only error handler middleware

License

NotificationsYou must be signed in to change notification settings

expressjs/errorhandler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

NPM VersionNPM DownloadsTest Coverage

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 standardError object, the string provided by thestack property will be returned in HTML/text responses.
  • When the object is a non-Error object, 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.

Install

This is aNode.js module available through thenpm registry. Installation is done using thenpm install command:

$ npm install errorhandler

API

varerrorhandler=require('errorhandler')

errorhandler(options)

Create new middleware to handle errors and respond with content negotiation.

Options

Error handler accepts these properties in the options object.

log

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.

Examples

Simple example

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())}

Custom output location

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})}

License

MIT

About

Development-only error handler middleware

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors15


    [8]ページ先頭

    ©2009-2025 Movatter.jp