summary refs log tree commit diff
path: root/api/src/util/ApiError.ts
diff options
context:
space:
mode:
authorAlTech98 <altech123159@gmail.com>2021-08-31 17:56:38 +0200
committerAlTech98 <altech123159@gmail.com>2021-08-31 17:56:38 +0200
commit7baa2fbe52bc2302fe6dcb062594316944c87a92 (patch)
tree1d398263ec9235d2433e6ef7edd8b0493798c272 /api/src/util/ApiError.ts
parentMerge pull request #297 from AlTech98/typeorm (diff)
downloadserver-7baa2fbe52bc2302fe6dcb062594316944c87a92.tar.xz
Created list of all possible api errors and made them throwable in routes code
Diffstat (limited to 'api/src/util/ApiError.ts')
-rw-r--r--api/src/util/ApiError.ts23
1 files changed, 23 insertions, 0 deletions
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 +}