Skip to content

Commit 27965ab

Browse files
author
Aaron Cook
committed
fix: fallback to statusCode in error message
1 parent 8dd3683 commit 27965ab

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/utils.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
export type Params = Record<string, string | number | boolean | null>
22

3-
export type ErrorResponse = {
4-
code: number
5-
message: string
6-
}
3+
export type ErrorResponse =
4+
| {
5+
code: number
6+
statusCode?: never
7+
message: string
8+
}
9+
| {
10+
code?: never
11+
statusCode: number
12+
message: string
13+
}
714

815
const isErrorResponse = (data: unknown): data is ErrorResponse => {
916
const isObject = typeof data === 'object' && data !== null
10-
return isObject && 'code' in data && 'message' in data
17+
return isObject && ('code' in data || 'statusCode' in data) && 'message' in data
1118
}
1219

1320
function replaceParam(str: string, key: string, value: string): string {
@@ -48,7 +55,7 @@ async function parseResponse<T>(resp: Response): Promise<T> {
4855

4956
if (!resp.ok) {
5057
const errTxt = isErrorResponse(json)
51-
? `CGW error - ${json.code}: ${json.message}`
58+
? `CGW error - ${json.code ?? json.statusCode}: ${json.message}`
5259
: `CGW error - status ${resp.statusText}`
5360
throw new Error(errTxt)
5461
}

0 commit comments

Comments
 (0)