diff options
Diffstat (limited to 'app/Exceptions')
-rw-r--r-- | app/Exceptions/Handler.js | 48 | ||||
-rw-r--r-- | app/Exceptions/Handler.ts | 23 |
2 files changed, 23 insertions, 48 deletions
diff --git a/app/Exceptions/Handler.js b/app/Exceptions/Handler.js deleted file mode 100644 index 43c3ef1..0000000 --- a/app/Exceptions/Handler.js +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | const BaseExceptionHandler = use('BaseExceptionHandler'); | ||
2 | const Sentry = require('@sentry/node'); | ||
3 | |||
4 | /** | ||
5 | * This class handles all exceptions thrown during | ||
6 | * the HTTP request lifecycle. | ||
7 | * | ||
8 | * @class ExceptionHandler | ||
9 | */ | ||
10 | class ExceptionHandler extends BaseExceptionHandler { | ||
11 | /** | ||
12 | * Handle exception thrown during the HTTP lifecycle | ||
13 | * | ||
14 | * @method handle | ||
15 | * | ||
16 | * @param {Object} error | ||
17 | * @param {Object} options.request | ||
18 | * @param {Object} options.response | ||
19 | * | ||
20 | * @return {void} | ||
21 | */ | ||
22 | async handle(error, { response }) { | ||
23 | if (error.name === 'ValidationException') { | ||
24 | return response.status(400).send('Invalid arguments'); | ||
25 | } if (error.name === 'InvalidSessionException') { | ||
26 | return response.status(401).redirect('/user/login'); | ||
27 | } | ||
28 | |||
29 | return response.status(error.status).send(error.message); | ||
30 | } | ||
31 | |||
32 | /** | ||
33 | * Report exception for logging or debugging. | ||
34 | * | ||
35 | * @method report | ||
36 | * | ||
37 | * @param {Object} error | ||
38 | * @param {Object} options.request | ||
39 | * | ||
40 | * @return {void} | ||
41 | */ | ||
42 | async report(error) { | ||
43 | Sentry.captureException(error); | ||
44 | return true; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | module.exports = ExceptionHandler; | ||
diff --git a/app/Exceptions/Handler.ts b/app/Exceptions/Handler.ts new file mode 100644 index 0000000..35c77d0 --- /dev/null +++ b/app/Exceptions/Handler.ts | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | |-------------------------------------------------------------------------- | ||
3 | | Http Exception Handler | ||
4 | |-------------------------------------------------------------------------- | ||
5 | | | ||
6 | | AdonisJs will forward all exceptions occurred during an HTTP request to | ||
7 | | the following class. You can learn more about exception handling by | ||
8 | | reading docs. | ||
9 | | | ||
10 | | The exception handler extends a base `HttpExceptionHandler` which is not | ||
11 | | mandatory, however it can do lot of heavy lifting to handle the errors | ||
12 | | properly. | ||
13 | | | ||
14 | */ | ||
15 | |||
16 | import Logger from '@ioc:Adonis/Core/Logger'; | ||
17 | import HttpExceptionHandler from '@ioc:Adonis/Core/HttpExceptionHandler'; | ||
18 | |||
19 | export default class ExceptionHandler extends HttpExceptionHandler { | ||
20 | constructor() { | ||
21 | super(Logger); | ||
22 | } | ||
23 | } | ||