-
Notifications
You must be signed in to change notification settings - Fork 45
Open
Description
Description
The onError hook is not triggered when a network-level error occurs (e.g., ECONNREFUSED, DNS failures, timeout at network level). Instead, the error is thrown as an uncaught exception.
This is inconsistent with the behavior for HTTP errors (4xx, 5xx), where onError is properly called and the error is returned as { data: null, error: ... }.
Expected Behavior
Network errors should:
- Trigger the
onErrorhook - Return
{ data: null, error: ... }(consistent with HTTP errors) - NOT throw an exception (unless
throw: trueis set)
Actual Behavior
Network errors:
- Do NOT trigger the
onErrorhook - Throw an uncaught
TypeError: fetch failedexception - Break the error-as-value pattern
Minimal Reproduction
import { createFetch } from "@better-fetch/fetch";
let onErrorCalled = false;
const $fetch = createFetch({
baseURL: "http://localhost:9999", // non-existent server
onError(context) {
onErrorCalled = true;
console.log("onError triggered:", context.error);
},
});
try {
const { data, error } = await $fetch("/test");
console.log("Result:", { data, error, onErrorCalled });
} catch (e) {
console.log("Exception thrown:", e.message);
console.log("onError was called:", onErrorCalled);
}Output:
Exception thrown: fetch failed
onError was called: false
Environment
- @better-fetch/fetch: 1.1.21
- Node.js: 22.14.0
- OS: Linux
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels