Files
directus/packages/errors
ian f5dcd85082 Add user limits (#22479)
* Limit users

* Add defaults

* Allow specifying of reason for limit exceeded error

* Update config options doc

* Make existing tests pass

* Update extensions limit error

* Update usage of regular expression

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>

* Rename typo

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>

* Rename files

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>

* Use first() and remove redundant +=

* Fix incorrect api access count

* Rework to account for passing of unchanged access values

* Fix increased counts in updateMany

* Consistent error message

* Simplify unnecessary find

* Add tests

* Rename UserCount to AccessTypeCount

* renamed env var

* prettier

* Add changeset

* fix limit checking for batch and status updates

* test and prettier

* removed obsolete check

* updated error

* fixed error usage in extenions service

* Use randomUUID from '@directus/random'

* Fix payload check in updateMany

* implemented RolesService.updateBatch from its parent

* resolved unit test error

* updated type

* fixed existing role query

* Temporary activation of blackbox tests

* Move to separate fn, to make skippable for non-existent role

* Revert "Temporary activation of blackbox tests"

This reverts commit 4c4ac846d6.

* Revert "Move to separate fn, to make skippable for non-existent role"

This reverts commit 1d90a82e39.

* Add user limits - extension (#22642)

* adressing existing users issue

* migrated changes from pascal

* only check the role for active users

* only count active users

* updated incorrect if

* default to count zero

* Undid abstraction to separate function

* fixed updating through user counting error

* prettier

* simplified fallback query

* prettier

* Added try catch to be safee

* updated db mocking for tests

* removed extra check to satisfy implementation tests

---------

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
Co-authored-by: Brainslug <tim@brainslug.nl>
Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
2024-06-04 17:00:17 +02:00
..
2024-06-04 17:00:17 +02:00
2024-05-28 17:01:27 +00:00
2023-11-20 16:23:22 +01:00

@directus/errors

Utility functions to help creating and checking against Directus errors.

Installation

pnpm add @directus/errors

Usage

Creating a new Error class

createError(code: string, message: string | (T) => string, status = 500): DirectusErrorConstructor<T>
import { createError } from '@directus/errors';

const ForbiddenError = createError('FORBIDDEN', "You don't have permissions to see this.", 403);

throw new ForbiddenError();

You can configure additional extensions for the error class which should be communicated to the end user:

import { createError } from '@directus/errors';

interface ForbiddenErrorExtensions {
	collection: string;
	field: string;
}

const ForbiddenError = createError<ForbiddenErrorExtensions>(
	'FORBIDDEN',
	"You don't have permissions to see this.",
	403,
);

throw new ForbiddenError({
	collection: 'articles',
	field: 'title',
});

You can then also use those extensions to generate out the error message:

import { createError } from '@directus/errors';

interface ForbiddenErrorExtensions {
	collection: string;
	field: string;
}

const messageConstructor = (extensions: ForbiddenErrorExtensions) =>
	`You don't have permissions to see "${extensions.field}" in "${extensions.collection}".`;

const ForbiddenError = createError<ForbiddenErrorExtensions>('FORBIDDEN', messageConstructor, 403);

throw new ForbiddenError({
	collection: 'articles',
	field: 'title',
});

Checking if a given param is a valid DirectusError instance

import { isDirectusError, createError } from '@directus/errors';

const ForbiddenError = createError('FORBIDDEN', "You don't have permissions to see this.", 403);

isDirectusError(new ForbiddenError()); // true
isDirectusError(new Error()); // false