OpsError
Error handling made in simple for your favorite nodejs framework.
Features
- Easy to use.
- Easy configuration.
- Custom throw error.
- Debugging and logging.
- Support Commonjs, ES6+ and Typescript.
- Also support Native Nodejs, Express, Koa, Fastify, Hapi and more.
Installation
$ npm install ops-error//or$ yarn add ops-error
Usage (example for native nodejs http server)
Of course, OpsError support for other framework you can find code in example folder or this wiki.
const http = ;const ops = ;// or esm and typescript// import ops from 'ops-error'; // it's using wrap asynchronous.const app = http; // if using try and catch block.const app = http; // simple configops; app; // example response error if access url /bad :// {// "statusCode": 400,// "name": "BadRequestError",// "message": "Bad request error for url /bad"// }
Wiki for framework
- Express error handling with ops-error
- Koa error handling with ops-error
- Fastify error handling with ops-error
- Hapi error handling with ops-error
- Restify error handling with ops-error
Debugging And Logging
... ops; // example response error if debug = true :// {// "statusCode": 404,// "name": "NotFoundError",// "message": "User Not Found",// "debug": {// "stack": [// "at Z:\\nodejs\\ops-error\\example\\express\\index.js:23:19"// ],// "request": {// "method": "GET",// "uri": "/user",// "headers": {// "user-agent": "PostmanRuntime/7.26.5",// "accept": "*/*",// "postman-token": "7e6ee0e1-c692-40db-b71b-7284c80e22bb",// "host": "localhost:3000",// "accept-encoding": "gzip, deflate, br",// "connection": "keep-alive"// }// },// "httpCode": 404// }// }
Custom throw error
// PaymentRequiredError.js const OpsError = ; { return 402 }; { return 'PaymentRequiredError' }; moduleexports = PaymentRequiredError;
const http = ;const config getError wrap = ;const PaymentRequiredError = ; const app = http; ; app;
Example Config
... const ops = ; ops; ...
List Error Available In This Library
Class | Code |
---|---|
BadRequestError |
400 |
UnauthorizedError |
401 |
ForbiddenError |
403 |
NotFoundError |
404 |
MethodNotAllowedError |
405 |
RequestTimeoutError |
408 |
ConflictError |
409 |
UnsupportedMediaTypeError |
415 |
UnprocessableEntityError |
422 |
InternalServerError |
500 |
NotImplementedError |
501 |
BadGatewayError |
502 |
ServiceUnavailableError |
503 |
Method
Name | Description |
---|---|
ops.config |
Configure ops error. Example => ops.config({ yourconfig }) |
ops.getError |
Display error status, name and message. Example => const data = ops.getError(err, req?); |
ops.print |
print in terminal. Example => ops.print(data) |
ops.wrap |
Wrap an asynchronous without try and catch. Example => ops.wrap((req, res) => res;) |
ops.next |
Optional if an function using try and catch block. Example => ops.next(err, req, res) |
Contact me : herudi7@gmail.com