Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions content/_partials/extensions-api-internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
11 changes: 11 additions & 0 deletions content/guides/09.extensions/2.api-extensions/2.endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
```