Partly refactor errors

This commit is contained in:
rubikscraft
2022-07-19 14:54:02 +02:00
parent c8722d8944
commit d507fcfaf0
15 changed files with 138 additions and 95 deletions

View File

@@ -10,7 +10,7 @@ export enum FT {
SysValidation = 'sysvalidation',
UsrValidation = 'usrvalidation',
Permission = 'permission',
NotFound = 'notFound',
NotFound = 'notfound',
Conflict = 'conflict',
Internal = 'internal',
Authentication = 'authentication',
@@ -21,6 +21,7 @@ export enum FT {
interface FTProp {
important: boolean;
code: number;
message: string;
}
const FTProps: {
@@ -29,56 +30,68 @@ const FTProps: {
[FT.Unknown]: {
important: false,
code: 500,
message: 'An unkown error occurred',
},
[FT.Internal]: {
important: true,
code: 500,
message: 'An internal error occurred',
},
[FT.Database]: {
important: true,
code: 500,
message: 'A database error occurred',
},
[FT.Network]: {
important: true,
code: 500,
message: 'A network error occurred',
},
[FT.SysValidation]: {
important: true,
code: 500,
message: 'Validation of internal items failed',
},
[FT.UsrValidation]: {
important: false,
code: 400,
message: 'Validation of user input failed',
},
[FT.Permission]: {
important: false,
code: 403,
message: 'Permission denied',
},
[FT.NotFound]: {
important: false,
code: 404,
message: 'Item(s) could not be found',
},
[FT.Conflict]: {
important: false,
code: 409,
message: 'There was a conflict',
},
[FT.Authentication]: {
important: false,
code: 200,
} ,
message: 'Authentication failed',
},
[FT.Impossible]: {
important: true,
code: 422,
} ,
message: 'What you are doing is impossible',
},
};
export class Failure {
private __68351953531423479708__id_failure = 1148363914;
constructor(
private readonly type: FT = FT.Unknown,
private readonly reason?: string,
private readonly stack?: string,
private readonly type: FT = FT.Unknown,
private readonly debugMessage?: string,
) {}
getReason(): string {
@@ -89,6 +102,10 @@ export class Failure {
return this.stack;
}
getDebugMessage(): string | undefined {
return this.debugMessage;
}
getType(): FT {
return this.type;
}
@@ -112,19 +129,34 @@ export class Failure {
throw new Error('Invalid failure data');
}
return new Failure(data.reason, data.stack, data.type);
return new Failure(data.type, data.reason, data.stack, data.debugMessage);
}
}
export function Fail(type: FT, reason: any): Failure {
if (typeof reason === 'string') {
return new Failure(reason, undefined, type);
} else if (reason instanceof Error) {
return new Failure(reason.message, reason.stack, type);
} else if (reason instanceof Failure) {
export function Fail(type: FT, reason?: any, dbgReason?: any): Failure {
const strReason = reason.toString();
if (typeof dbgReason === 'string') {
return new Failure(type, strReason, undefined, dbgReason);
} else if (dbgReason instanceof Error) {
return new Failure(type, strReason, dbgReason.stack, dbgReason.message);
} else if (dbgReason instanceof Failure) {
throw new Error('Cannot fail with a failure, just return it');
} else {
return new Failure('Unkown reason', undefined, type);
if (typeof reason === 'string') {
return new Failure(type, strReason, undefined, undefined);
} else if (reason instanceof Error) {
return new Failure(
type,
FTProps[type].message,
reason.stack,
reason.message,
);
} else if (dbgReason instanceof Failure) {
throw new Error('Cannot fail with a failure, just return it');
} else {
return new Failure(type, FTProps[type].message, undefined, undefined);
}
}
}

View File

@@ -13,5 +13,5 @@ export function ParseMime(mime: string): Failable<FullMime> {
if (SupportedAnimMimes.includes(mime))
return { mime, type: SupportedMimeCategory.Animation };
return Fail(FT.Validation, 'Unsupported mime type');
return Fail(FT.UsrValidation, 'Unsupported mime type');
}