express-rest-errors

1.1.4 • Public • Published

Express REST APi Error-Handler

What is it?

It provides a minimalistic error-hanlding middleware layer to your express REST-API, Web App, or anything else by formatting error-data and sending JSON in response

Example Response :

{
    "error": {
        "type": "Bad Request",
        "message": "Invalid data provided", 
        "payload": { "name" : "Invalid name" }
    }
}

Motivation

Are you also tired of having to re-write simple, yet annoying error-hadnling middleware for your API, but existing packages are to robust or lack configuration for you? Try this one:

  • Zero-Configuration
  • Minimalistic
  • Full-Customizable

Getting started

Intall : npm install express-rest-errors

Set Up:

const express=require("express")
const errors=require("express-rest-errors")
const app=express()

// ... Some express config

app.use(errors())

app.listen(PORT)

Throw error :

throw errors.error( 
    400, // Status-code - 500 by default
    "Message", // Error message - Optional
    {...}, // Payload error data - Optional
) 

And that`s it! Now some examples of more advanced usage

Advanced Configuration

app.use(errors({
    default : {...}, // Default payload data
    codes : {
        400 : "[Title]", // Title for error with particular error code
        ...
    },
    message : {
        400 : "[Message]", // Default messages for errors with particular error code
        ...
    }
}))

License

MIT

Package Sidebar

Install

npm i express-rest-errors

Weekly Downloads

1

Version

1.1.4

License

ISC

Last publish

Collaborators

  • greg-pevnev