Skip to content

Commit

Permalink
fix(ua): add better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed May 19, 2021
1 parent 5600d4d commit 45ef899
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/api/lib/api-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
}

0 comments on commit 45ef899

Please sign in to comment.