diff --git a/content/_partials/extensions-api-internals.md b/content/_partials/extensions-api-internals.md index 32677073..52060e0e 100644 --- a/content/_partials/extensions-api-internals.md +++ b/content/_partials/extensions-api-internals.md @@ -11,9 +11,10 @@ Learn more about using internal Directus services. To create errors in API extensions, you can utilize the [`@directus/errors`](https://www.npmjs.com/package/@directus/errors) package which is available to all extensions without installation. ```js -import { createError } from '@directus/errors'; +import { createError, ForbiddenError } from '@directus/errors'; -const ForbiddenError = createError('FORBIDDEN', "You don't have permissions to see this.", 403); +const CustomError = createError('CUSTOM', "This is a custom error.", 418); throw new ForbiddenError(); +throw new CustomError(); ``` diff --git a/content/guides/09.extensions/2.api-extensions/2.endpoints.md b/content/guides/09.extensions/2.api-extensions/2.endpoints.md index daf98c71..27a3be4a 100644 --- a/content/guides/09.extensions/2.api-extensions/2.endpoints.md +++ b/content/guides/09.extensions/2.api-extensions/2.endpoints.md @@ -88,3 +88,14 @@ Learn more about the Directus sandbox for API extensions. :: :partial{content="extensions-api-internals"} + +Inside a route, error like this to make sure that the api doesn't crash. + +```ts +router.get('/my-endpoint', async (req, res, next) => { + if(checkForError) { + next(new ForbiddenError()) + return + } +}) +```