1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00
unleash.unleash/src/lib/error/from-legacy-error.ts
Christopher Kolstad 6673d131fe
feat: biome lint (#4853)
This commit changes our linter/formatter to biome (https://biomejs.dev/)
Causing our prehook to run almost instantly, and our "yarn lint" task to
run in sub 100ms.

Some trade-offs:
* Biome isn't quite as well established as ESLint
* Are we ready to install a different vscode plugin (the biome plugin)
instead of the prettier plugin


The configuration set for biome also has a set of recommended rules,
this is turned on by default, in order to get to something that was
mergeable I have turned off a couple the rules we seemed to violate the
most, that we also explicitly told eslint to ignore.
2023-09-29 14:18:21 +02:00

123 lines
3.3 KiB
TypeScript

import { fromJoiError } from './bad-data-error';
import { ValidationError as JoiValidationError } from 'joi';
import {
GenericUnleashError,
UnleashApiErrorName,
UnleashApiErrorTypes,
UnleashError,
} from './unleash-error';
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;
}
};
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';
const statusCode = getStatusCode(name);
if (name === 'NoAccessError') {
return new GenericUnleashError({
name: 'NoAccessError',
message: e.message,
statusCode,
});
}
if (e instanceof JoiValidationError) {
return fromJoiError(e);
}
if (name === 'ValidationError' || name === 'BadDataError') {
return new GenericUnleashError({
name: 'BadDataError',
message: e.message,
statusCode,
});
}
if (name === 'OwaspValidationError') {
return new GenericUnleashError({
name: 'OwaspValidationError',
message: e.message,
statusCode,
});
}
if (name === 'AuthenticationRequired') {
return new GenericUnleashError({
name: 'AuthenticationRequired',
message: `You must be authenticated to view this content. Please log in.`,
statusCode,
});
}
return new GenericUnleashError({
name: name as UnleashApiErrorName,
message: e.message,
statusCode,
});
};