chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
import { fromJoiError } from './bad-data-error';
|
|
|
|
import { ValidationError as JoiValidationError } from 'joi';
|
|
|
|
import {
|
|
|
|
GenericUnleashError,
|
|
|
|
UnleashApiErrorName,
|
|
|
|
UnleashApiErrorTypes,
|
|
|
|
UnleashError,
|
|
|
|
} from './unleash-error';
|
|
|
|
|
2023-07-11 09:20:11 +02:00
|
|
|
const getStatusCode = (errorName: string): number => {
|
|
|
|
switch (errorName) {
|
|
|
|
case 'ContentTypeError':
|
|
|
|
return 415;
|
|
|
|
case 'ValidationError':
|
|
|
|
return 400;
|
|
|
|
case 'BadDataError':
|
|
|
|
return 400;
|
|
|
|
case 'OwaspValidationError':
|
|
|
|
return 400;
|
|
|
|
case 'PasswordUndefinedError':
|
|
|
|
return 400;
|
|
|
|
case 'MinimumOneEnvironmentError':
|
|
|
|
return 400;
|
|
|
|
case 'InvalidTokenError':
|
|
|
|
return 401;
|
|
|
|
case 'UsedTokenError':
|
|
|
|
return 403;
|
|
|
|
case 'InvalidOperationError':
|
|
|
|
return 403;
|
|
|
|
case 'IncompatibleProjectError':
|
|
|
|
return 403;
|
|
|
|
case 'OperationDeniedError':
|
|
|
|
return 403;
|
|
|
|
case 'NotFoundError':
|
|
|
|
return 404;
|
|
|
|
case 'NameExistsError':
|
|
|
|
return 409;
|
|
|
|
case 'FeatureHasTagError':
|
|
|
|
return 409;
|
|
|
|
case 'RoleInUseError':
|
|
|
|
return 400;
|
|
|
|
case 'ProjectWithoutOwnerError':
|
|
|
|
return 409;
|
|
|
|
case 'UnknownError':
|
|
|
|
return 500;
|
|
|
|
case 'InternalError':
|
|
|
|
return 500;
|
|
|
|
case 'PasswordMismatch':
|
|
|
|
return 401;
|
|
|
|
case 'UnauthorizedError':
|
|
|
|
return 401;
|
|
|
|
case 'DisabledError':
|
|
|
|
return 422;
|
|
|
|
case 'NotImplementedError':
|
|
|
|
return 405;
|
|
|
|
case 'NoAccessError':
|
|
|
|
return 403;
|
|
|
|
case 'AuthenticationRequired':
|
|
|
|
return 401;
|
|
|
|
case 'ForbiddenError':
|
|
|
|
return 403;
|
|
|
|
case 'PermissionError':
|
|
|
|
return 403;
|
|
|
|
case 'BadRequestError': //thrown by express; do not remove
|
|
|
|
return 400;
|
|
|
|
default:
|
|
|
|
return 500;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
export const fromLegacyError = (e: Error): UnleashError => {
|
|
|
|
if (e instanceof UnleashError) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
const name = UnleashApiErrorTypes.includes(e.name as UnleashApiErrorName)
|
|
|
|
? (e.name as UnleashApiErrorName)
|
|
|
|
: 'UnknownError';
|
|
|
|
|
2023-07-11 09:20:11 +02:00
|
|
|
const statusCode = getStatusCode(name);
|
|
|
|
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
if (name === 'NoAccessError') {
|
|
|
|
return new GenericUnleashError({
|
|
|
|
name: 'NoAccessError',
|
|
|
|
message: e.message,
|
2023-07-11 09:20:11 +02:00
|
|
|
statusCode,
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e instanceof JoiValidationError) {
|
|
|
|
return fromJoiError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === 'ValidationError' || name === 'BadDataError') {
|
|
|
|
return new GenericUnleashError({
|
|
|
|
name: 'BadDataError',
|
|
|
|
message: e.message,
|
2023-07-11 09:20:11 +02:00
|
|
|
statusCode,
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === 'OwaspValidationError') {
|
|
|
|
return new GenericUnleashError({
|
|
|
|
name: 'OwaspValidationError',
|
|
|
|
message: e.message,
|
2023-07-11 09:20:11 +02:00
|
|
|
statusCode,
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === 'AuthenticationRequired') {
|
|
|
|
return new GenericUnleashError({
|
|
|
|
name: 'AuthenticationRequired',
|
|
|
|
message: `You must be authenticated to view this content. Please log in.`,
|
2023-07-11 09:20:11 +02:00
|
|
|
statusCode,
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new GenericUnleashError({
|
|
|
|
name: name as UnleashApiErrorName,
|
|
|
|
message: e.message,
|
2023-07-11 09:20:11 +02:00
|
|
|
statusCode,
|
chore: improve joi errors (#3836)
This PR improves our handling of internal Joi errors, to make them more
sensible to the end user. It does that by providing a better description
of the errors and by telling the user what they value they provided was.
Previous conversion:
```json
{
"id": "705a8dc0-1198-4894-9015-f1e5b9992b48",
"name": "BadDataError",
"message": "\"value\" must contain at least 1 items",
"details": [
{
"message": "\"value\" must contain at least 1 items",
"description": "\"value\" must contain at least 1 items"
}
]
}
```
New conversion:
```json
{
"id": "87fb4715-cbdd-48bb-b4d7-d354e7d97380",
"name": "BadDataError",
"message": "Request validation failed: your request body contains invalid data. Refer to the `details` list for more information.",
"details": [
{
"description": "\"value\" must contain at least 1 items. You provided [].",
"message": "\"value\" must contain at least 1 items. You provided []."
}
]
}
```
## Restructuring
This PR moves some code out of `unleash-error.ts` and into a new file.
The purpose of this is twofold:
1. avoid circular dependencies (we need imports from both UnleashError
and BadDataError)
2. carve out a clearer separation of concerns, keeping `unleash-error` a
little more focused.
2023-06-07 10:29:36 +02:00
|
|
|
});
|
|
|
|
};
|