mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Add on-error hook (#4233)
* Add on-error hook Closes #2906 * Ignore unspecified listeners * Document error hook
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
import { EventEmitter2 } from 'eventemitter2';
|
||||
import logger from './logger';
|
||||
|
||||
const emitter = new EventEmitter2({ wildcard: true, verboseMemoryLeak: true, delimiter: '.' });
|
||||
const emitter = new EventEmitter2({
|
||||
wildcard: true,
|
||||
verboseMemoryLeak: true,
|
||||
delimiter: '.',
|
||||
|
||||
// This will ignore the "unspecified event" error
|
||||
ignoreErrors: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Emit async events without throwing errors. Just log them out as warnings.
|
||||
|
||||
@@ -3,6 +3,7 @@ import { BaseException } from '../exceptions';
|
||||
import logger from '../logger';
|
||||
import env from '../env';
|
||||
import { toArray } from '../utils/to-array';
|
||||
import { emitAsyncSafe } from '../emitter';
|
||||
|
||||
const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
|
||||
let payload: any = {
|
||||
@@ -66,7 +67,9 @@ const errorHandler: ErrorRequestHandler = (err, req, res, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
return res.json(payload);
|
||||
emitAsyncSafe('error', payload.errors).then(() => {
|
||||
return res.json(payload);
|
||||
});
|
||||
};
|
||||
|
||||
export default errorHandler;
|
||||
|
||||
@@ -81,8 +81,9 @@ module.exports = function registerHook({ exceptions }) {
|
||||
| `middlewares.init` | `before` and `after` | No |
|
||||
| `request` | `not_found` | No |
|
||||
| `response` | | No† |
|
||||
| `items` | `create`, `update` and `delete` | Optional |
|
||||
| `error` | | No |
|
||||
| `auth` | `success`†, `fail`† and `refresh`† | No |
|
||||
| `items` | `create`, `update` and `delete` | Optional |
|
||||
| `activity` | `create`, `update` and `delete` | Optional |
|
||||
| `collections` | `create`, `update` and `delete` | Optional |
|
||||
| `fields` | `create`, `update` and `delete` | Optional |
|
||||
|
||||
Reference in New Issue
Block a user