Forward framework-caught errors to Datadog
Open this in step 5. The RUM Browser SDK automatically captures uncaught browser errors (window.onerror and unhandledrejection), but frontend frameworks catch component and rendering errors in their own boundaries or handlers. Wire only the detected framework below, and never report the same error twice.
Contents
- React (plain React, not Next.js)
- Next.js (App Router and Pages Router)
- Vue (plain Vue, not Nuxt)
- Nuxt
- Angular
- SvelteKit
- Plain Svelte, vanilla JS, or CDN
React (plain React, not Next.js)
- Existing class error boundary (
componentDidCatch): preserve the boundary and its fallback UI. InsidecomponentDidCatch(error, errorInfo), calladdReactError(error, errorInfo)from@datadog/browser-rum-reactwhen that package is installed, ordatadogRum.addError(error, { componentStack: errorInfo?.componentStack })from@datadog/browser-rum. - Existing React 19
createRooterror callbacks (onCaughtError/onUncaughtError): calladdReactErrorordatadogRum.addErrorinside the existing callback and preserve its current logic. - Already using Datadog
ErrorBoundaryoraddReactError: make no change. - No existing error boundary: if
@datadog/browser-rum-reactis installed, wrap the root application component inErrorBoundaryfrom@datadog/browser-rum-reactwith a simplefallbackcomponent (({ error, resetError }) => ...). If@datadog/browser-rum-reactis not installed, add a small React class error boundary around the root<App />whosecomponentDidCatch(error, errorInfo)callsdatadogRum.addError(error, { componentStack: errorInfo?.componentStack }), so no extra package install is required.
Next.js (App Router and Pages Router)
- App Router (
app/orsrc/app/): report from both root boundaries. The rooterror.tsxcatches errors in route segments but not in the rootlayout.tsx; onlyglobal-error.tsxcatches those.For each of
error.tsxandglobal-error.tsx(or.jsx) that already exists in the App Router root, keep its UI andresetbutton, and add auseEffectkeyed on[error]that reports the error:- When
@datadog/browser-rum-nextjsis installed, importaddNextjsErrorfrom@datadog/browser-rum-nextjsand calladdNextjsError(error). - Otherwise, import
datadogRumfrom@datadog/browser-rumand calldatadogRum.addError(error).
- When
If no
error.tsxexists in the App Router root (app/orsrc/app/), createerror.tsx(or.jsxin a JavaScript project) with'use client'at the top:'use client'; import { useEffect } from 'react'; import { datadogRum } from '@datadog/browser-rum'; export default function Error({ error, reset, }: { error: Error & { digest?: string }; reset: () => void; }) { useEffect(() => { datadogRum.addError(error); }, [error]); return ( <div> <h2>Something went wrong</h2> <button type="button" onClick={() => reset()}>Try again</button> </div> ); }If no
global-error.tsxexists in the App Router root, create it from the same template, name the componentGlobalError, and wrap the returned markup in<html>and<body>, because it replaces the root layout when it renders.In both files, when
@datadog/browser-rum-nextjsis installed inpackage.json, useaddNextjsError(error)from@datadog/browser-rum-nextjsinstead ofdatadogRum.addError(error). Always pass the originalerrorobject so Next.js server-componentdigestmetadata is preserved.
- Pages Router (
pages/_app): if a custom error boundary exists, calldatadogRum.addError(error)incomponentDidCatch; otherwise wrap<Component {...pageProps} />in a small error boundary that callsdatadogRum.addError(error)incomponentDidCatch.
Vue (plain Vue, not Nuxt)
After const app = createApp(...) and before app.mount(...):
When
@datadog/browser-rum-vueis installed, importaddVueErrorfrom@datadog/browser-rum-vueand setapp.config.errorHandler = addVueError(or calladdVueError(err, instance, info)inside an existingapp.config.errorHandler).When only
@datadog/browser-rumis installed, wireapp.config.errorHandler:app.config.errorHandler = (err, _instance, info) => { datadogRum.addError(err, { vueErrorInfo: info }); };If
app.config.errorHandleralready exists, preserve its behavior and add thedatadogRum.addErrorcall inside it.
Nuxt
- When
@datadog/browser-rum-nuxtis used, ensurenuxtRumPlugin({ router: useRouter(), nuxtApp: useNuxtApp() })passesnuxtApp. - Otherwise, in the client-only Datadog Nuxt plugin (
plugins/*.client.ts), registernuxtApp.vueApp.config.errorHandlerto calldatadogRum.addError(err, { vueErrorInfo: info }).
Angular
When
@datadog/browser-rum-angularis installed:- With no custom
ErrorHandler, registerprovideDatadogErrorHandler()from@datadog/browser-rum-angularin thebootstrapApplicationor@NgModuleprovidersarray. - With an existing custom
ErrorHandler, calladdAngularError(error)inside itshandleError(error)method.
- With no custom
When only
@datadog/browser-rumis installed, register anErrorHandlerprovider in the existing bootstrap or moduleprovidersarray:import { ErrorHandler } from '@angular/core'; import { datadogRum } from '@datadog/browser-rum'; class DatadogErrorHandler implements ErrorHandler { handleError(error: unknown): void { datadogRum.addError(error); console.error(error); } }Add
{ provide: ErrorHandler, useClass: DatadogErrorHandler }toproviders(or calldatadogRum.addError(error)inside the app's existing customErrorHandler).
SvelteKit
In src/hooks.client.ts (or src/hooks.client.js), export handleError so router and rendering errors are forwarded to Datadog:
import type { HandleClientError } from '@sveltejs/kit';
import { datadogRum } from '@datadog/browser-rum';
export const handleError: HandleClientError = ({ error, status }) => {
if (status !== 404) {
datadogRum.addError(error);
}
};If src/hooks.client.* already exports handleError, preserve its existing logic and add datadogRum.addError(error) inside it.
Plain Svelte, vanilla JS, or CDN
Plain Svelte (Vite without SvelteKit), vanilla JavaScript, and CDN setups do not have a framework error boundary that swallows exceptions before window.onerror. Leave their application code unchanged once datadogRum.init and the application switch are verified.