Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@ezee/http-errors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@ezee/http-errors
Create beautiful and consistent HTTP errors with ease.
Install
With npm
npm install @ezee/http-errorsWith yarn
yarn add @ezee/http-errorsWith pnpm
pnpm add @ezee/http-errorsUsage
import httpErrors from '@ezee/http-errors';
const error = new httpErrors.Notfound({
message: 'User not found',
code: 'USER_NOT_FOUND',
meta: {
userId: 123,
},
});How to instanciate an error
const error = new httpErrors.<ErrorName>(options);Options
message- The error message.code- The error code.meta- Additional information about the error. Inobjectformat. (optional)
Available Errors
- BadRequest
- Unauthorized
- Forbidden
- Notfound
- MethodNotAllowed
- Conflict
- InternalServerError
- NotImplemented
- ServiceUnavailable
- GatewayTimeout
- PaymentRequired
Examples
With express
import express from 'express';
import httpErrors from '@ezee/http-errors';
const app = express();
app.get('/user/:id', (req, res, next) => {
const { id } = req.params;
if (id !== '123') {
return next(
new httpErrors.Notfound({
message: 'User not found',
code: 'USER_NOT_FOUND',
meta: {
userId: id,
},
})
);
}
res.json({ id, name: 'John Doe' });
});With koa
import Koa from 'koa';
import httpErrors from '@ezee/http-errors';
const app = new Koa();
app.use(async (ctx, next) => {
const { id } = ctx.params;
if (id !== '123') {
ctx.throw(
new httpErrors.Notfound({
message: 'User not found',
code: 'USER_NOT_FOUND',
meta: {
userId: id,
},
})
);
}
ctx.body = { id, name: 'John Doe' };
});