Skip to content

Commit ff32bd2

Browse files
fix: adjust ErrorFromResponse error class (#1491)
BREAKING CHANGE: `ErrorFromResponse` class constructor now requires second parameter (`status`, `response` and optionally `code`)
1 parent dcb1030 commit ff32bd2

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/client.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,20 +1181,17 @@ export class StreamChat {
11811181
});
11821182
}
11831183

1184-
errorFromResponse(
1185-
response: AxiosResponse<APIErrorResponse>,
1186-
): ErrorFromResponse<APIErrorResponse> {
1187-
let err: ErrorFromResponse<APIErrorResponse>;
1188-
err = new ErrorFromResponse(`StreamChat error HTTP code: ${response.status}`);
1189-
if (response.data && response.data.code) {
1190-
err = new Error(
1191-
`StreamChat error code ${response.data.code}: ${response.data.message}`,
1192-
);
1193-
err.code = response.data.code;
1194-
}
1195-
err.response = response;
1196-
err.status = response.status;
1197-
return err;
1184+
errorFromResponse(response: AxiosResponse<APIErrorResponse>) {
1185+
const message =
1186+
typeof response.data.code !== 'undefined'
1187+
? `StreamChat error code ${response.data.code}: ${response.data.message}`
1188+
: `StreamChat error HTTP code: ${response.status}`;
1189+
1190+
return new ErrorFromResponse<APIErrorResponse>(message, {
1191+
code: response.data.code ?? null,
1192+
response,
1193+
status: response.status,
1194+
});
11981195
}
11991196

12001197
handleResponse<T>(response: AxiosResponse<T>) {

src/types.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,18 +3197,36 @@ type ErrorResponseDetails = {
31973197
};
31983198

31993199
export type APIErrorResponse = {
3200-
code: number;
32013200
duration: string;
32023201
message: string;
32033202
more_info: string;
32043203
StatusCode: number;
3204+
code?: number;
32053205
details?: ErrorResponseDetails;
32063206
};
32073207

32083208
export class ErrorFromResponse<T> extends Error {
3209-
code?: number;
3210-
response?: AxiosResponse<T>;
3211-
status?: number;
3209+
public code: number | null;
3210+
public status: number;
3211+
public response: AxiosResponse<T>;
3212+
3213+
constructor(
3214+
message: string,
3215+
{
3216+
code,
3217+
status,
3218+
response,
3219+
}: {
3220+
code: ErrorFromResponse<T>['code'];
3221+
response: ErrorFromResponse<T>['response'];
3222+
status: ErrorFromResponse<T>['status'];
3223+
},
3224+
) {
3225+
super(message);
3226+
this.code = code;
3227+
this.response = response;
3228+
this.status = status;
3229+
}
32123230
}
32133231

32143232
export type QueryPollsResponse = {

0 commit comments

Comments
 (0)