yeps-error
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
.editorconfig
.eslintrc
.gitignore
.istanbul.yml
.travis.yml
LICENSE
README.md
appveyor.yml
index.js
package.json

README.md

YEPS Error Handler

NPM

npm version Build Status Coverage Status Linux Build Windows Build

Dependency Status devDependency Status NSP Status

License GitHub stars GitHub forks GitHub issues Twitter

Simple 404/500 error handler for YEPS app

Installation

npm i -S yeps-error

How to use

const App = require('yeps');
const app = new App();

const error = require('yeps-error');

app.all([
    error(),
]);

Or

app.then(error());

Config

JSON request

 app.all([
     // res.setHeader('Content-Type', 'application/json')
     error({ isJSON: true }),
 ]);

Hide user error (404)

 app.all([
     error({ hasUserError: false }),
 ]);

Hide server error (500)

 app.all([
     error({ hasServerError: false }),
 ]);

With logger

const logger = require('yeps-logger');

app.all([
    logger(),
    error(),
]);

How to make custom error handler

// create app
const App = require('yeps');
const app = new App();
const Router = require('yeps.router');
const router = new Router();

// add middlewares
app.all([...]);

// add router
router.catch().then(async (ctx) => {
    ctx.res.statusCode = 200;
    ctx.res.end('Homepage');
});

app.then(router.resolve());

// 404 error handler
app.then(async (ctx) => {
    ctx.res.statusCode = 404;
    ctx.res.end('Not Found');
});

// 500 error handler
app.catch(async (err, ctx) => {
    ctx.res.statusCode = 500;
    ctx.res.end('Internal Server Error');
});

HTTP response message and status code

app.then(async (ctx) => {
    const err = new Error();
    err.code = 403;
    
    return Promise.reject(err);
});

And you will see http response 403 with message Forbidden.

YEPS documentation