summary refs log tree commit diff
path: root/api/src/util/ApiError.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 18:13:16 +0200
committerGitHub <noreply@github.com>2021-08-31 18:13:16 +0200
commita95c7aa59c320396b7d2eedd4354316ef167de5d (patch)
treecfec2fdf2a37066197937dbca32ceb89a038477b /api/src/util/ApiError.ts
parentMerge branch 'typeorm' of https://github.com/fosscord/fosscord-api into typeorm (diff)
parentMerge branch 'typeorm' into typeorm (diff)
downloadserver-a95c7aa59c320396b7d2eedd4354316ef167de5d.tar.xz
Merge pull request #299 from AlTech98/typeorm
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 +}