Socket
Socket
Sign inDemoInstall

express-default-errors

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-default-errors

Default ExpressJS error-handling middleware.


Version published
Weekly downloads
3
Maintainers
1
Install size
6.93 kB
Created
Weekly downloads
 

Readme

Source

express-default-errors

Default ExpressJS error-handling middleware.

Installation

$ npm install express-default-errors

Usage

express-default-errors provides simple middleware to catch default errors and serve custom errors.

var express = require('express')
var errorHandler = require('express-default-errors')

var app = express()
var router = express.Router()

// Valid route that will not cause any errors
router.get('/', function (req, res, next) {
  res.status(200)
  res.send('success')
})

// To signify unauthorized access, simple create a new Error object with status = 401
router.get('/restricted-route', function (req, res, next) {
  var err = new Error()
  err.status = 401
  next(err)
})

// Custom error message
router.get('/error-route', function (req, res, next) {
  // Will default to err.status = 500 unless err.status is explicitly set
  var err = new Error('A custom error was caused.')
  next(err)
})

// This is not recommended, but you could also set non-standard status codes.
// The default error message will be 'Internal Server Error' unless set.
router.get('/invalid-status-code-error-route', function (req, res, next) {
  var err = new Error()
  err.status = 299
  next(err)
})

// Any errors will be sent with the appropriate status code by the response object as:
// {error: 'My error message here'}

// The error handler should be used after all routers
app.use(router)
app.use(errorHandler())

app.listen(3000)

Default Errors

Setting the status code will cause the middleware to serve default error messages when calling new Error() without passing in any arguments, or if err.message is not set.

Status CodeDefault Message
400Bad Request
401Unauthorized
404Not Found
500Internal Server Error

License

MIT

FAQs

Last updated on 27 Aug 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc