Rework hook registration (#8027)

* Rework hook registration

* Remove event and action fields from hook payloads

* Move "error" action to "request.error" filter

* Emit meta and context objects in filters and actions

* Run filters sequentially

* Update hook templates

* Fix CLI hook test

* Also emit `<collection>.items.crud` when emitting `items.crud`.

* Update hook docs

Co-authored-by: Oreilles <oreilles.github@nitoref.io>
Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
Nicola Krumschmidt
2021-11-03 22:18:56 +01:00
committed by GitHub
parent 0aa5026aa9
commit 3d0e086c55
19 changed files with 629 additions and 429 deletions

View File

@@ -1,10 +1,11 @@
import { ErrorRequestHandler } from 'express';
import { emitAsyncSafe } from '../emitter';
import emitter from '../emitter';
import env from '../env';
import { MethodNotAllowedException } from '../exceptions';
import { BaseException } from '@directus/shared/exceptions';
import logger from '../logger';
import { toArray } from '@directus/shared/utils';
import getDatabase from '../database';
// Note: keep all 4 parameters here. That's how Express recognizes it's the error handler, even if
// we don't use next
@@ -87,9 +88,20 @@ const errorHandler: ErrorRequestHandler = (err, req, res, _next) => {
}
}
emitAsyncSafe('error', payload.errors).then(() => {
return res.json(payload);
});
emitter
.emitFilter(
'request.error',
payload.errors,
{},
{
database: getDatabase(),
schema: req.schema,
accountability: req.accountability ?? null,
}
)
.then(() => {
return res.json(payload);
});
};
export default errorHandler;