The error reporting API allows you to manually report errors to telemetry. While the SDK automatically captures unhandled errors and unhandled promise rejections, this API is useful for reporting handled errors, custom error conditions, and framework-specific errors (like React error boundaries).
Critical Error Reporting Behavior:
ignoreErrorMessages patterns are not reportedinit() may be queued or droppedManually reports an error to be tracked in telemetry.
/**
* Manually reports an error to be tracked in telemetry
* @param error - Error message or error object
* @param opts - Error reporting options (optional)
* @throws No errors thrown; invalid errors may be ignored
*/
function reportError(error: string | ErrorLike, opts?: ReportErrorOpts): void;
interface ErrorLike {
message: string;
name?: string;
stack?: string;
}
interface ReportErrorOpts {
/** Component stack trace for React errors */
componentStack?: string | null | undefined;
/** Additional attributes to include with the error report */
attributes?: Record<string, AttributeValueType | AnyValue>;
}Usage Examples:
import { reportError } from "@dash0/sdk-web";
// Report a string error
reportError("Something went wrong in user flow");
// Report an Error object
try {
riskyOperation();
} catch (error) {
reportError(error);
}
// Report with additional context
reportError("Failed to load user data", {
attributes: {
"user.id": "user-12345",
"operation": "fetch_profile",
"retry_count": 3,
},
});
// Report with custom error object
reportError({
message: "Network request failed",
name: "NetworkError",
stack: "Error: Network request failed\n at fetchData (app.js:123)",
});For IIFE (script tag) usage:
dash0("reportError", "Something went wrong");
dash0("reportError", error);Critical Behavior:
ignoreErrorMessages patterns are not reportedReport caught exceptions that you handle in your code:
import { reportError } from "@dash0/sdk-web";
async function loadUserData(userId) {
try {
const response = await fetch(`/api/users/${userId}`);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return await response.json();
} catch (error) {
// Report the error
reportError(error, {
attributes: {
"user.id": userId,
"error.context": "user_data_fetch",
},
});
// Show user-friendly error
showErrorMessage("Failed to load user data");
// Return fallback data
return null;
}
}Report errors from promise chains:
fetchData()
.then(processData)
.catch((error) => {
reportError(error, {
attributes: {
"operation": "data_processing",
},
});
});Use in React error boundaries to report component errors:
import React from "react";
import { reportError } from "@dash0/sdk-web";
class ErrorBoundary extends React.Component {
componentDidCatch(error, errorInfo) {
// Report the error with component stack
reportError(error, {
componentStack: errorInfo.componentStack,
attributes: {
"error.boundary": this.constructor.name,
},
});
}
render() {
if (this.state.hasError) {
return <ErrorFallback />;
}
return this.props.children;
}
}For React 18+ with functional error boundaries:
import { reportError } from "@dash0/sdk-web";
function App() {
return (
<ErrorBoundary
onError={(error, errorInfo) => {
reportError(error, {
componentStack: errorInfo.componentStack,
});
}}
>
<MyComponent />
</ErrorBoundary>
);
}Report validation failures with context:
function validateForm(formData) {
if (!formData.email.includes("@")) {
reportError("Invalid email format", {
attributes: {
"form.id": "registration",
"field.name": "email",
"validation.rule": "email_format",
},
});
return false;
}
return true;
}Report API-specific errors with response details:
async function callAPI(endpoint, data) {
try {
const response = await fetch(endpoint, {
method: "POST",
body: JSON.stringify(data),
});
if (!response.ok) {
const errorBody = await response.text();
reportError(`API Error: ${response.status}`, {
attributes: {
"http.url": endpoint,
"http.status_code": response.status,
"http.response_body": errorBody,
"api.endpoint": endpoint,
},
});
}
return response.json();
} catch (error) {
reportError(error, {
attributes: {
"api.endpoint": endpoint,
"error.type": "network",
},
});
throw error;
}
}Report application-specific error conditions:
function processPayment(amount, paymentMethod) {
if (amount > MAX_TRANSACTION_AMOUNT) {
reportError("Transaction amount exceeds limit", {
attributes: {
"transaction.amount": amount,
"transaction.limit": MAX_TRANSACTION_AMOUNT,
"payment.method": paymentMethod,
},
});
throw new Error("Amount exceeds maximum allowed");
}
// Process payment...
}Report errors from third-party libraries:
import { reportError } from "@dash0/sdk-web";
// Stripe error handling
stripe.confirmPayment(options)
.catch((error) => {
reportError(error, {
attributes: {
"integration": "stripe",
"payment.intent": paymentIntentId,
"error.code": error.code,
"error.decline_code": error.decline_code,
},
});
});
// Analytics error handling
analytics.track("event").catch((error) => {
reportError(error, {
attributes: {
"integration": "analytics",
"event.name": "custom_event",
},
});
});Report custom error types:
class AuthenticationError extends Error {
constructor(message, userId) {
super(message);
this.name = "AuthenticationError";
this.userId = userId;
}
}
try {
authenticateUser(credentials);
} catch (error) {
if (error instanceof AuthenticationError) {
reportError(error, {
attributes: {
"error.type": "authentication",
"user.id": error.userId,
},
});
} else {
reportError(error);
}
}Report operation timeouts:
async function fetchWithTimeout(url, timeout = 5000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, { signal: controller.signal });
clearTimeout(timeoutId);
return response;
} catch (error) {
clearTimeout(timeoutId);
if (error.name === "AbortError") {
reportError("Request timeout", {
attributes: {
"url": url,
"timeout.ms": timeout,
"error.type": "timeout",
},
});
} else {
reportError(error);
}
throw error;
}
}Common attributes to include with error reports:
user.id - User identifier for user-specific debuggingerror.context - Context where the error occurredoperation - Operation being performedretry_count - Number of retry attemptserror.type - Category of error (network, validation, etc.)component - Component or module nameerror.recoverable - Whether the error is recoverableThe SDK automatically tracks:
wrapEventHandlers: true)wrapTimers: true)Use reportError() for:
message, behavior is undefinedreportError() is called before init(), errors may be queued or droppedinit() is called multiple times, errors are sent to all configured endpointsignoreErrorMessages patterns are not reported