summary refs log tree commit diff
path: root/src/util/util/ApiError.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/util/ApiError.ts')
-rw-r--r--src/util/util/ApiError.ts28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util/util/ApiError.ts b/src/util/util/ApiError.ts
new file mode 100644

index 00000000..f1a9b4f6 --- /dev/null +++ b/src/util/util/ApiError.ts
@@ -0,0 +1,28 @@ +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 | number)[]): ApiError { + return new ApiError(applyParamsToString(this.message, params), this.code, this.httpStatus); + } +} + +export function applyParamsToString(s: string, params: (string | number)[]): string { + let newString = s; + params.forEach((a) => { + newString = newString.replace("{}", "" + a); + }); + return newString; +}