1 | # AdonisJs Generic Exceptions 🚀
|
2 | > Customized exceptions for AdonisJs
|
3 |
|
4 | [![NPM Version][npm-image]][npm-url]
|
5 |
|
6 | This 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
|
9 | Install package using npm.
|
10 |
|
11 | ```shell
|
12 | npm i @adonisjs/generic-exceptions
|
13 | ```
|
14 |
|
15 | ## Usage
|
16 | The 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
|
19 | const { InvalidArgumentException } = require('@adonisjs/generic-exceptions')
|
20 |
|
21 | const message = 'Model.create requires an object'
|
22 | const status = 400
|
23 | const code = 'E_INVALID_ARGUMENT'
|
24 |
|
25 | throw new InvalidArgumentException(message, status, code)
|
26 | ```
|
27 |
|
28 | The `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
|
31 | The 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 |
|
38 | Checkout 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
|