summary refs log tree commit diff
path: root/api/src/util/ApiError.ts
blob: 2316cd71dad6c8c5e9fef0532f8f5def6a5a82c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
}