Skip to content

Commit 093c19b

Browse files
committed
Support HTTP 457 response :)
1 parent 15c93bd commit 093c19b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/templates/core/ApiError.hbs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export class ApiError extends Error {
99
public readonly statusText: string;
1010
public readonly body: any;
1111
public readonly request: ApiRequestOptions;
12+
public readonly code?: string;
1213

13-
constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
14+
constructor(request: ApiRequestOptions, response: ApiResult, message: string, code?: string) {
1415
super(message);
1516

1617
this.name = 'ApiError';
@@ -19,5 +20,6 @@ export class ApiError extends Error {
1920
this.statusText = response.statusText;
2021
this.body = response.body;
2122
this.request = request;
23+
this.code = code;
2224
}
2325
}

src/templates/core/functions/catchErrorCodes.hbs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
66
401: 'Unauthorized',
77
403: 'Forbidden',
88
404: 'Not Found',
9+
457: 'Coauthor Error',
910
500: 'Internal Server Error',
1011
502: 'Bad Gateway',
1112
503: 'Service Unavailable',
@@ -14,7 +15,14 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
1415

1516
const error = errors[result.status];
1617
if (error) {
17-
throw new ApiError(options, result, error);
18+
const code = result.status === 457 &&
19+
typeof result.body === 'object' &&
20+
result.body !== null &&
21+
'code' in result.body
22+
? result.body.code
23+
: undefined;
24+
25+
throw new ApiError(options, result, error, code);
1826
}
1927

2028
if (!result.ok) {

0 commit comments

Comments
 (0)