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
+}
|