Skip to content

Abort errors are not caught from response.text() #1893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flevi29 opened this issue Mar 17, 2025 · 0 comments · Fixed by #1749
Closed

Abort errors are not caught from response.text() #1893

flevi29 opened this issue Mar 17, 2025 · 0 comments · Fixed by #1749
Labels
bug Something isn't working

Comments

@flevi29
Copy link
Collaborator

flevi29 commented Mar 17, 2025

/**
* Sends a request with {@link fetch} or a custom HTTP client, combining
* parameters and class properties.
*
* @returns A promise containing the response
*/
async #request<T = unknown>({
path,
method,
params,
contentType,
body,
extraRequestInit,
}: MainRequestOptions): Promise<T> {
const url = new URL(path, this.#url);
if (params !== undefined) {
appendRecordToURLSearchParams(url.searchParams, params);
}
const requestInit: RequestInit = {
method,
body:
contentType === undefined || typeof body !== "string"
? JSON.stringify(body)
: body,
...extraRequestInit,
...this.#requestInit,
headers: this.#getHeaders(extraRequestInit?.headers, contentType),
};
const startTimeout =
this.#requestTimeout !== undefined
? getTimeoutFn(requestInit, this.#requestTimeout)
: null;
const getResponseAndHandleErrorAndTimeout = <
U extends ReturnType<NonNullable<Config["httpClient"]> | typeof fetch>,
>(
responsePromise: U,
stopTimeout?: ReturnType<NonNullable<typeof startTimeout>>,
) =>
responsePromise
.catch((error: unknown) => {
throw new MeiliSearchRequestError(
url.toString(),
Object.is(error, TIMEOUT_OBJECT)
? new Error(`request timed out after ${this.#requestTimeout}ms`, {
cause: requestInit,
})
: error,
);
})
.finally(() => stopTimeout?.()) as U;
const stopTimeout = startTimeout?.();
if (this.#customRequestFn !== undefined) {
const response = await getResponseAndHandleErrorAndTimeout(
this.#customRequestFn(url, requestInit),
stopTimeout,
);
// When using a custom HTTP client, the response should already be handled and ready to be returned
return response as T;
}
const response = await getResponseAndHandleErrorAndTimeout(
fetch(url, requestInit),
stopTimeout,
);
const responseBody = await response.text();
const parsedResponse =
responseBody === "" ? undefined : JSON.parse(responseBody);
if (!response.ok) {
throw new MeiliSearchApiError(response, parsedResponse);
}
return parsedResponse as T;
}

@flevi29 flevi29 changed the title Abort errors are not caught from [response.text()](https://developer.mozilla.org/en-US/docs/Web/API/Response/text) Abort errors are not caught from response.text() Mar 17, 2025
@flevi29 flevi29 added the bug Something isn't working label Mar 17, 2025
@flevi29 flevi29 linked a pull request Mar 17, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant