All files / lib report-error.ts

100% Statements 4/4
100% Branches 2/2
100% Functions 2/2
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                            2x             5x             2x 1x    
import type { ClientOnErrorFunction } from 'react-router';
 
/** Where an error that reached `reportError` came from. */
interface ErrorContext {
  readonly source: 'route' | 'query' | 'mutation';
  /** The route pattern (`/items/:id`) or the query key, as text. */
  readonly where?: string;
}
 
/**
 * The one exit for an error the app did not expect: a route that threw, an API answer the app
 * cannot use (a 5xx, a body that breaks its schema). It writes to the console. To send errors to
 * a tracker (Sentry, Datadog, ...), call it here: every caller then reports there.
 */
export const reportError = (
  // The inbound boundary: a route can throw anything, and nothing here reads the value but the
  // console (or a tracker), so it stays unknown.
  // oxlint-disable-next-line anti-slop/no-unknown-parameters
  error: unknown,
  context: ErrorContext,
) => {
  console.error(`[${context.source}${context.where ? ` ${context.where}` : ''}]`, error);
};
 
/**
 * RouterProvider's `onError` (main.tsx): every error a route throws, once (RouteError may render
 * it more than once). React Router does not call it for a URL that matches no route.
 */
export const reportRouteError: ClientOnErrorFunction = (error, { pattern }) => {
  reportError(error, { source: 'route', where: pattern });
};