Skip to content

Commit

Permalink
feat!: reformated onError interceptor's error object to be consistent…
Browse files Browse the repository at this point in the history
… with the one from callapi
  • Loading branch information
Ryan-Zayne committed Sep 13, 2024
1 parent 210abd2 commit a620848
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-hornets-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zayne-labs/callapi": minor
---

reformated onError interceptor's error object to be consistent with the one from callapi
12 changes: 6 additions & 6 deletions src/createFetchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
ExtraOptions,
FetchConfig,
GetCallApiResult,
ResultModeUnion,
ResultModeUnion
} from "./types";
import { isObject, isQueryString, isString } from "./utils/typeof";
import {
Expand Down Expand Up @@ -236,8 +236,7 @@ export const createFetchClient = <

// == Also call the onError interceptor
options.onError?.({
error: null,
errorData,
error,
options,
request: requestInit,
response,
Expand All @@ -247,21 +246,22 @@ export const createFetchClient = <
return resolveErrorResult(error);
}

const errorResult = resolveErrorResult();

void (await Promise.all([
// == At this point only the request errors exist, so the request error interceptor is called
options.onRequestError?.({ error: error as Error, options, request: requestInit }),

// == Also call the onError interceptor
options.onError?.({
error: error as Error,
errorData: null,
error: (errorResult as { error: never }).error,
options,
request: requestInit,
response: null,
}),
]));

return resolveErrorResult();
return errorResult;

// == Removing the now unneeded AbortController from store
} finally {
Expand Down
30 changes: 16 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,13 @@ export type ResponseErrorContext<TErrorData> = Prettify<{

export type ErrorContext<TErrorData> =
| {
error: Error;
errorData?: null;
error: Extract<ErrorObjectUnion, { name: PossibleErrorNames }>;
options: ExtraOptions;
request: $RequestOptions;
response: null;
}
| {
error: null;
errorData?: TErrorData;
error: Extract<ErrorObjectUnion<TErrorData>, { name: "HTTPError" }>;
options: ExtraOptions;
request: $RequestOptions;
response: Response;
Expand All @@ -247,23 +245,27 @@ type PossibleErrorNames = {
_: "AbortError" | "Error" | "SyntaxError" | "TimeoutError" | "TypeError" | "UnknownError";
}["_"];

type ErrorObjectUnion<TErrorData = unknown> =
| {
errorData: Error;
message: string;
name: PossibleErrorNames;
}
| {
errorData: TErrorData;
message: string;
name: "HTTPError";
};

export type ApiErrorVariant<TErrorData> =
| {
data: null;
error: {
errorData: Error;
message: string;
name: PossibleErrorNames;
};
error: Extract<ErrorObjectUnion, { name: PossibleErrorNames }>;
response: null;
}
| {
data: null;
error: {
errorData: TErrorData;
message: string;
name: "HTTPError";
};
error: Extract<ErrorObjectUnion<TErrorData>, { name: "HTTPError" }>;
response: Response;
};

Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export const getResolveErrorResultFn = <CallApiResult>(initInfo: {
}) => {
const { error, options } = initInfo;

const resolveErrorResult = (errorInfo: Partial<HTTPError<unknown>> = {}): CallApiResult => {
const { errorData, message, response } = errorInfo;
const resolveErrorResult = <TErrorData>(errorInfo?: Partial<HTTPError<TErrorData>>): CallApiResult => {
const { errorData, message, response } = errorInfo ?? {};

const shouldThrowOnError = isFunction(options.throwOnError)
? options.throwOnError(error as Error)
Expand Down
8 changes: 6 additions & 2 deletions website/pages/advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,13 @@ const callAnotherApi = callApi.create({
`onError` is invoked both on request and response errors.
It is basically a combination of `onRequestError` and `onResponseError` in that it provides access to:
- The `errorData` (if the error a response error from api, else it's set to null).
- The response object (f the error is a response error from api, else it's set to null).
- The `error` object (if the error is a request error, else it's set to null).
- The `error` object which contains the following properties (just like the error object destructured from callApi itself):
1. `name`: A string indicating the type of error (e.g., 'TypeError', 'SyntaxError', 'HTTPError').
2. `message`: The error message describing what went wrong.
3. `errorData`: The error data, which can be an error response from the API or a standard JavaScript error object..
- The request details.
- And finally the fetch options used.
Expand Down

0 comments on commit a620848

Please sign in to comment.