Sentry
HyperFetch reports failures through the response instead of throwing, so a failed request never reaches Sentry's global
error handler on its own. One onError interceptor on the client sends them to Sentry, with the method, the endpoint and
the status attached. There is no Sentry package to install beyond Sentry's own SDK.
- Every failed request reported: One interceptor covers every request of the client, including generated SDKs.
- Grouped by endpoint: Failures of
/users/:idland in one issue, not one per user id. - Tracing included: Sentry's
fetchinstrumentation already records each call as a span and a breadcrumb.
Report failed requests
Initialize Sentry first, then add an error interceptor to your client:
import * as Sentry from "@sentry/react";
import { AbortError, createClient } from "@hyper-fetch/core";
export const client = createClient({ url: "https://api.example.com" });
client.onError((response, request) => {
// Cancelled requests and expected 4xx answers are not bugs
const isServerError = !response.status || response.status >= 500;
if (response.error instanceof AbortError || !isServerError) return response;
// The endpoint before params were filled in, like /users/:id
const { endpoint } = request.requestOptions;
Sentry.captureException(
new Error(
`${request.method} ${endpoint} failed with status ${response.status}`,
),
{
fingerprint: ["hyper-fetch", request.method, String(endpoint)],
tags: {
method: request.method,
endpoint: String(endpoint),
status: String(response.status),
},
extra: { error: response.error },
},
);
return response;
});
The interceptor must return the response, so the request still resolves with it. Status 0 means the request never got
an answer: a network failure or a timeout.
Use the Sentry SDK for your platform: @sentry/react, @sentry/nextjs, @sentry/node, @sentry/bun or
@sentry/react-native. The interceptor is the same on all of them.
Tracing and breadcrumbs
HyperFetch sends requests with the global fetch, which Sentry instruments. With tracing on, every request shows up as
an http.client span and a breadcrumb, with no extra code. To connect frontend spans to your backend, list your API in
tracePropagationTargets, so Sentry adds its trace headers to those requests:
import * as Sentry from "@sentry/react";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
integrations: [Sentry.browserTracingIntegration()],
tracesSampleRate: 0.2,
tracePropagationTargets: ["https://api.example.com"],
});
Your API has to allow the sentry-trace and baggage headers in its CORS settings.
- You want to know when an endpoint starts failing before users report it.
- You need failures grouped by endpoint, with the method and status as searchable tags.
- You already trace your backend with Sentry and want frontend requests in the same trace.
client.onErrorsends failed requests to Sentry, skipping cancelled ones and expected 4xx answers.request.requestOptions.endpointgroups failures by endpoint template.- Sentry's
fetchinstrumentation adds spans and breadcrumbs for every request on its own.
