1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-31 00:16:47 +01:00
unleash.unleash/src/lib/error/permission-error.ts
Christopher Kolstad 53354224fc
chore: Bump biome and configure husky (#6589)
Upgrades biome to 1.6.1, and updates husky pre-commit hook.

Most changes here are making type imports explicit.
2024-03-18 13:58:05 +01:00

41 lines
1.2 KiB
TypeScript

import { type ApiErrorSchema, UnleashError } from './unleash-error';
type Permission = string | string[];
class PermissionError extends UnleashError {
statusCode = 403;
permissions: Permission;
constructor(permission: Permission = [], environment?: string) {
const permissions = Array.isArray(permission)
? permission
: [permission];
const permissionsMessage =
permissions.length === 1
? `the "${permissions[0]}" permission`
: `one of the following permissions: ${permissions
.map((perm) => `"${perm}"`)
.join(', ')}`;
const message = `You don't have the required permissions to perform this operation. To perform this action, you need ${permissionsMessage}${
environment ? ` in the "${environment}" environment.` : `.`
}`;
super(message);
this.permissions = permissions;
}
toJSON(): ApiErrorSchema {
return {
...super.toJSON(),
permissions: this.permissions,
};
}
}
export default PermissionError;
module.exports = PermissionError;