|
1 | 1 | import {
|
2 | 2 | AspiError,
|
3 |
| - type CustomError, |
| 3 | + CustomError, |
4 | 4 | type AspiRequest,
|
5 | 5 | type AspiResponse,
|
| 6 | + type JSONParseError, |
6 | 7 | } from './error';
|
7 | 8 | import {
|
8 | 9 | getHttpErrorStatus,
|
@@ -408,15 +409,17 @@ export class Request<
|
408 | 409 | | (Opts extends { error: any }
|
409 | 410 | ? Opts['error'][keyof Opts['error']]
|
410 | 411 | : never)
|
411 |
| - | CustomError<'jsonParseError', { message: string }> |
| 412 | + | JSONParseError |
412 | 413 | >
|
413 | 414 | > {
|
414 | 415 | 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 | + }), |
420 | 423 | ),
|
421 | 424 | );
|
422 | 425 | }
|
@@ -479,8 +482,14 @@ export class Request<
|
479 | 482 | while (attempts <= retries) {
|
480 | 483 | try {
|
481 | 484 | response = await fetch(url, requestInit);
|
| 485 | + |
482 | 486 | responseData = await responseParser(response);
|
483 | 487 |
|
| 488 | + if (responseData instanceof CustomError) { |
| 489 | + // @ts-ignore |
| 490 | + return Result.err(responseData); |
| 491 | + } |
| 492 | + |
484 | 493 | if (
|
485 | 494 | response.ok ||
|
486 | 495 | (!retryOn.includes(response.status as HttpErrorCodes) &&
|
@@ -511,10 +520,13 @@ export class Request<
|
511 | 520 | });
|
512 | 521 |
|
513 | 522 | // @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 | + ); |
518 | 530 | }
|
519 | 531 |
|
520 | 532 | if (attempts < retries) {
|
@@ -562,10 +574,13 @@ export class Request<
|
562 | 574 | });
|
563 | 575 |
|
564 | 576 | // @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 | + ); |
569 | 584 | }
|
570 | 585 |
|
571 | 586 | return Result.err(
|
|
0 commit comments