From 8141cb553bfe8255977586d798d40297df0acf55 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Mon, 6 Oct 2025 13:41:46 +0200 Subject: [PATCH 1/2] improve error handling examples --- content/_partials/extensions-api-internals.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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(); ``` From c5e433c472d82edab6f4b309a2eff0defd8798f7 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Mon, 6 Oct 2025 14:09:00 +0200 Subject: [PATCH 2/2] tweak example --- .../09.extensions/2.api-extensions/2.endpoints.md | 11 +++++++++++ 1 file changed, 11 insertions(+) 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 + } +}) +```