Skip to content

Commit 14fe5be

Browse files
Add toJSON instead
1 parent 6e8bc10 commit 14fe5be

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
@@ -3217,7 +3217,8 @@ export type APIErrorResponse = {
32173217
export class ErrorFromResponse<T> extends Error {
32183218
public code: number | null;
32193219
public status: number;
3220-
public response: Pick<AxiosResponse<T>, 'data' | 'status' | 'statusText' | 'headers'>;
3220+
public response: AxiosResponse<T>;
3221+
public name = 'ErrorFromResponse';
32213222

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

32463265
export type QueryPollsResponse = {

0 commit comments

Comments
 (0)