From 7fcb68f0825b99a830f7c1ee24bd58a456c1ed5b Mon Sep 17 00:00:00 2001 From: AlTech98 Date: Tue, 31 Aug 2021 17:56:38 +0200 Subject: Created list of all possible api errors and made them throwable in routes code --- api/src/util/ApiError.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 api/src/util/ApiError.ts (limited to 'api/src/util/ApiError.ts') diff --git a/api/src/util/ApiError.ts b/api/src/util/ApiError.ts new file mode 100644 index 00000000..2316cd71 --- /dev/null +++ b/api/src/util/ApiError.ts @@ -0,0 +1,23 @@ +export class ApiError extends Error { + constructor(readonly message: string, public readonly code: number, public readonly httpStatus: number = 400, public readonly defaultParams?: string[]) { + super(message); + } + + withDefaultParams(): ApiError { + if(this.defaultParams) + return new ApiError(applyParamsToString(this.message, this.defaultParams), this.code, this.httpStatus) + return this + } + + withParams(...params: string[]): ApiError { + return new ApiError(applyParamsToString(this.message, params), this.code, this.httpStatus) + } +} + +export function applyParamsToString(s: string, params: string[]): string { + let newString = s + params.forEach(a => { + newString = newString.replace("{}", a) + }) + return newString +} -- cgit 1.5.1