Skip to content

Commit cca142f

Browse files
committed
fix: json error fix
1 parent d584c24 commit cca142f

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

.changeset/pre.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"popular-snakes-clean",
2121
"rare-knives-drive",
2222
"rich-pets-explain",
23+
"stale-dancers-sing",
2324
"strong-taxis-unite",
2425
"tough-monkeys-shave",
2526
"twenty-wasps-grow"

src/error.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,21 @@ export class AspiError<TReq extends AspiRequestInit> extends Error {
8080
}
8181
}
8282

83-
export interface CustomError<Tag extends string, A> {
83+
export class CustomError<Tag extends string, A> extends Error {
8484
tag: Tag;
8585
data: A;
86+
87+
constructor(tag: Tag, data: A) {
88+
super(tag);
89+
this.tag = tag;
90+
this.data = data;
91+
}
8692
}
93+
94+
export interface JSONParseError
95+
extends CustomError<
96+
'jsonParseError',
97+
{
98+
message: string;
99+
}
100+
> {}

src/request.ts

+30-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {
22
AspiError,
3-
type CustomError,
3+
CustomError,
44
type AspiRequest,
55
type AspiResponse,
6+
type JSONParseError,
67
} from './error';
78
import {
89
getHttpErrorStatus,
@@ -408,15 +409,17 @@ export class Request<
408409
| (Opts extends { error: any }
409410
? Opts['error'][keyof Opts['error']]
410411
: never)
411-
| CustomError<'jsonParseError', { message: string }>
412+
| JSONParseError
412413
>
413414
> {
414415
return this.#makeRequest<T>(async (response) =>
415-
response.json().catch((e) =>
416-
Result.err({
417-
data: e instanceof Error ? e.message : 'Failed to parse JSON',
418-
tag: 'jsonParseError',
419-
}),
416+
response.json().catch(
417+
(e) =>
418+
new CustomError('jsonParseError', {
419+
data: {
420+
message: e instanceof Error ? e.message : 'Failed to parse JSON',
421+
},
422+
}),
420423
),
421424
);
422425
}
@@ -479,8 +482,14 @@ export class Request<
479482
while (attempts <= retries) {
480483
try {
481484
response = await fetch(url, requestInit);
485+
482486
responseData = await responseParser(response);
483487

488+
if (responseData instanceof CustomError) {
489+
// @ts-ignore
490+
return Result.err(responseData);
491+
}
492+
484493
if (
485494
response.ok ||
486495
(!retryOn.includes(response.status as HttpErrorCodes) &&
@@ -511,10 +520,13 @@ export class Request<
511520
});
512521

513522
// @ts-ignore
514-
return Result.err({
515-
data: result,
516-
tag: this.#customErrorCbs[response.status].tag,
517-
});
523+
return Result.err(
524+
new CustomError(
525+
// @ts-ignore
526+
this.#customErrorCbs[response.status].tag,
527+
result,
528+
),
529+
);
518530
}
519531

520532
if (attempts < retries) {
@@ -562,10 +574,13 @@ export class Request<
562574
});
563575

564576
// @ts-ignore
565-
return Result.err({
566-
data: result,
567-
tag: this.#customErrorCbs[response!.status].tag,
568-
});
577+
return Result.err(
578+
new CustomError(
579+
// @ts-ignore
580+
this.#customErrorCbs[response!.status].tag,
581+
result,
582+
),
583+
);
569584
}
570585

571586
return Result.err(

0 commit comments

Comments
 (0)