Skip to content

Error handling express middleware for brinkbit custom errors

License

Notifications You must be signed in to change notification settings

Brinkbit/brinkbit-express-errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

brinkbit-express-errors

Express error handling middleware for http errors

Install

npm i --save brinkbit-express-errors

Overview

Designed for API usage, ingests brinkbit-custom-errors and any application errors that may have slipped through and sends the appropriate response to the client.

Usage

const errorWare = require( 'brinkbit-express-errors' );
const customErrors = require( 'brinkbit-custom-errors' );
const express = require( 'express' );

const app = express();

app.get( '/404', ( req, res, next ) => {
    next( new customErrors.NoResourceError({
        message: '404 for UI',
        type: 'ui_error',
    }));
});
app.get( '/api/404', ( req, res, next ) => {
    next( new customErrors.NoResourceError({
        message: 'This message will only be logged internally',
        description: 'This message will be forwarded to the client',
        type: customErrors.type.invalid_request_error,
        details: {
            _resource: customErrors.type.missing_resource,
        },
    }));
})

// if you want to handle errors with an alternate response (i.e. UI)
// simply drop that middleware higher in the stack and forward any other types of errors
app.use(( err, req, res, next )=> {
    if ( err.type !== 'ui_error' ) return next( err );
    else {
        // return UI
    }
});
app.use( errorWare );

About

Error handling express middleware for brinkbit custom errors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published