fix: sanitize timeout error message

This commit is contained in:
Alexandre Brandizzi 2026-07-14 21:23:59 -03:00
parent 18a5dbe058
commit 66ca109c77
2 changed files with 8 additions and 3 deletions

View file

@ -1,4 +1,4 @@
import { isHTTPError, isNetworkError } from "ky";
import { isHTTPError, isNetworkError, isTimeoutError } from "ky";
export function mapHttpStatusToMessage(status: number, data?: unknown): string {
if (status === 401) {
@ -36,5 +36,10 @@ export function normalizeApiRequestError(error: Error): Error {
return error;
}
if (isTimeoutError(error)) {
error.message = "The request timed out. Please try again.";
return error;
}
return error;
}

View file

@ -39,7 +39,7 @@ describe("normalizeApiRequestError", () => {
);
});
it("preserves the original message for timeouts", async () => {
it("returns a friendly timeout message", async () => {
const fetchImpl = vi.fn((_input: RequestInfo | URL, init?: RequestInit) => {
const signal = init?.signal;
return new Promise<Response>((_resolve, reject) => {
@ -48,7 +48,7 @@ describe("normalizeApiRequestError", () => {
}) as unknown as typeof fetch;
await expect(createClient(fetchImpl).get(RESOURCE_URL).json()).rejects.toThrow(
"Request timed out",
"The request timed out. Please try again.",
);
});
});