yeps-error
v1.3.1
Published
YEPS Error Handler
Downloads
16
Maintainers
Readme
YEPS Error Handler
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.