From 45ef8992ee49c55387a7eaf460fad0ba961fa0d6 Mon Sep 17 00:00:00 2001 From: wessberg Date: Wed, 19 May 2021 16:23:57 +0200 Subject: [PATCH] fix(ua): add better error handling --- src/api/lib/api-error.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/api/lib/api-error.ts b/src/api/lib/api-error.ts index d0a39e7..83936d4 100644 --- a/src/api/lib/api-error.ts +++ b/src/api/lib/api-error.ts @@ -35,16 +35,10 @@ export class ApiError extends Error { static ensureApiError(data: Error | ApiError | ApiErrorRecord): ApiError { if (data instanceof ApiError) return data; - if ("status" in data) return ApiError.fromJSON(data); + const status = "status" in data && typeof data.status === "number" ? data.status : StatusCodes.INTERNAL_SERVER_ERROR; + const message = data.message || ("name" in data ? data.name : undefined) || "Unknown Error"; + const stack = data.stack; - if ("status" in data && typeof status === "number") { - const castData = data as Error & {status: number}; - return ApiError.fromJSON(castData); - } else { - return ApiError.fromJSON({ - status: StatusCodes.INTERNAL_SERVER_ERROR, - ...data - }); - } + return ApiError.fromJSON({status, message, stack}); } }