Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

errdrop

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

errdrop

HTTP status enabled Error class maintaining backward compatibility

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
43
increased by48.28%
Maintainers
1
Weekly downloads
 
Created
Source

errdrop

A lightweight drop-in Error replacement with take-it-or-leave-it HTTP status code support.

Useful for preserving associations between errors and their appropriate HTTP status codes when decoupling application logic from middleware plumbing.

Installation

# npm
npm i errdrop

# yarn
yarn add errdrop

Example

const Error = require('errdrop')

// Good practice to decouple application logic
// from middleware plumbing (req/res)
function sayHello(name) {
  switch (name) {
    case 'Dwight':
      throw new Error.Forbidden(`Go away, ${name}.`)

    case 'Michael':
      throw new Error.MovedPermanently()

    default:
      return `Hello, ${name}!`
  }
}

app.get('/say-hello/:name', (req, res) => {
  try {
    res.send(sayHello(req.params.name))
  }
  catch (err) {
    res.status(err.statusCode || 500).send(err.message)
  }
})

Supported status codes

Subclasses are generated for each entry in http.STATUS_CODES. See here for the full list.

These classes pass all arguments through to Error for full drop-in compatibility.

Custom or nonstandard status codes are supported via the generic Error.StatusCode class, which prepends the Error class signature with a status code parameter:

throw new Error.StatusCode(218, 'This is fine')

thinware support

This module pairs well with thinware, which honors status codes attached to thrown Error objects:

/*
  /say-hello/Dwight   =>  403 Forbidden          "Go away, Dwight."
  /say-hello/Michael  =>  301 Moved Permanently  "Moved Permanently"
  /say-hello/Pam      =>  200 OK                 "Hello, Pam!"
*/
app.get('/say-hello/:name',
  thinware(sayHello, req => req.params.name)
)

Keywords

FAQs

Package last updated on 18 Feb 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc