UNPKG

1.61 kBMarkdownView Raw
1# AdonisJs Generic Exceptions 🚀
2> Customized exceptions for AdonisJs
3
4[![NPM Version][npm-image]][npm-url]
5
6This repo contains some helpful classes to throw uniform exceptions through out the application. Ofcourse you can throw exceptions using `new Error()` but using this package will help in throwing informative exceptions.
7
8## Setup
9Install package using npm.
10
11```shell
12npm i @adonisjs/generic-exceptions
13```
14
15## Usage
16The package exports all exceptions from [node-exceptions](https://poppinss.github.io/node-exceptions/docs/index.html) and adds additional methods to the `RuntimeException` class.
17
18```js
19const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
20
21const message = 'Model.create requires an object'
22const status = 400
23const code = 'E_INVALID_ARGUMENT'
24
25throw new InvalidArgumentException(message, status, code)
26```
27
28The `status` must be a valid HTTP status code and `code` is a unique error code to recognize an exception. AdonisJs error codes starts with `E_`, for example: `E_MISSING_CONFIG`.
29
30## RuntimeException
31The following static methods are added to the runtime exception to make the adonisJs related exceptions consistent.
32
33#### missingConfig(key, file)
34#### missingAppKey(providerName)
35#### incompleteConfig(missingKeys, file)
36#### invoke(message, status, [code])
37
38Checkout the complete API docs [here](https://adonisjs.github.io/adonis-generic-exceptions/docs/classes/main.runtimeexception.html)
39
40[npm-image]: https://img.shields.io/npm/v/@adonisjs/generic-exceptions.svg?style=flat-square
41[npm-url]: https://npmjs.org/package/@adonisjs/generic-exceptions