Skip to content

Commit 325d81c

Browse files
Add toJSON instead
1 parent accfd44 commit 325d81c

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/types.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,7 +3216,8 @@ export type APIErrorResponse = {
32163216
export class ErrorFromResponse<T> extends Error {
32173217
public code: number | null;
32183218
public status: number;
3219-
public response: Pick<AxiosResponse<T>, 'data' | 'status' | 'statusText' | 'headers'>;
3219+
public response: AxiosResponse<T>;
3220+
public name = 'ErrorFromResponse';
32203221

32213222
constructor(
32223223
message: string,
@@ -3226,20 +3227,38 @@ export class ErrorFromResponse<T> extends Error {
32263227
response,
32273228
}: {
32283229
code: ErrorFromResponse<T>['code'];
3229-
response: AxiosResponse<T>;
3230+
response: ErrorFromResponse<T>['response'];
32303231
status: ErrorFromResponse<T>['status'];
32313232
},
32323233
) {
32333234
super(message);
32343235
this.code = code;
3235-
this.response = {
3236-
data: response.data,
3237-
status: response.status,
3238-
statusText: response.statusText,
3239-
headers: response.headers,
3240-
};
3236+
this.response = response;
32413237
this.status = status;
32423238
}
3239+
3240+
// Vitest helper (serialized errors are too large to read)
3241+
// https://github.com/vitest-dev/vitest/blob/v3.1.3/packages/utils/src/error.ts#L60-L62
3242+
toJSON() {
3243+
const extra = [
3244+
['status', this.status],
3245+
['code', this.code],
3246+
] as const;
3247+
3248+
const joinable = [];
3249+
3250+
for (const [key, value] of extra) {
3251+
if (typeof value !== 'undefined' && value !== null) {
3252+
joinable.push(`${key}: ${value}`);
3253+
}
3254+
}
3255+
3256+
return {
3257+
message: `(${joinable.join(', ')}) - ${this.message}`,
3258+
stack: this.stack,
3259+
name: this.name,
3260+
};
3261+
}
32433262
}
32443263

32453264
export type QueryPollsResponse = {

0 commit comments

Comments
 (0)