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
onError hook
- Return
{ data: null, error: ... } (consistent with HTTP errors)
- NOT throw an exception (unless
throw: true is set)
Actual Behavior
Network errors:
- Do NOT trigger the
onError hook
- Throw an uncaught
TypeError: fetch failed exception
- 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
Description
The
onErrorhook 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
onErroris properly called and the error is returned as{ data: null, error: ... }.Expected Behavior
Network errors should:
onErrorhook{ data: null, error: ... }(consistent with HTTP errors)throw: trueis set)Actual Behavior
Network errors:
onErrorhookTypeError: fetch failedexceptionMinimal Reproduction
Output:
Environment