| title | Error Handling in the React SDK |
|---|---|
| order | 7 |
Error handling in the React SDK occurs across multiple layers:
- Top-Level Error Boundary — in
GustoProviderCustomUIAdapter.tsx - Component-Level Error Boundary — in
Base.tsx - Form Submission Error Processing — in
useBaseSubmit.ts - Observability Hooks — for error tracking (see Observability)
The SDK uses a unified SDKError type for all error scenarios:
interface SDKError {
category: 'api_error' | 'validation_error' | 'network_error' | 'internal_error'
message: string
httpStatus?: number
fieldErrors: SDKFieldError[]
raw?: unknown
}| Category | Description | Examples |
|---|---|---|
api_error |
HTTP error from the Gusto API | 422 validation errors, 404 not found, 409 conflict |
validation_error |
Client-side Zod schema failure | Request/response failed Zod validation before being sent |
network_error |
Network connectivity failure | Connection refused, timeout, request aborted |
internal_error |
Unexpected runtime error | Unhandled exceptions, initialization failures |
When a form submission fails, useBaseSubmit catches the error and normalizes it into an SDKError via normalizeToSDKError. This error is:
- Set as component state (
error) for UI rendering - Emitted to
observability.onErroras anObservabilityError(withtimestampandcomponentName)
For API errors with structured field errors (e.g. 422), the SDK extracts field-level errors into error.fieldErrors. Components like FederalTaxes, StateTaxes, and PaySchedule map these to inline form validation via react-hook-form.
Unexpected errors thrown during rendering are caught by the React ErrorBoundary in Base.tsx. These are:
- Rendered using the
InternalErrorcomponent (or a customFallbackComponent) - Emitted to
observability.onErrorwithcomponentNameandcomponentStack
A "Try again" button is shown, allowing users to attempt a re-render.
Errors that escape component-level boundaries are caught by the top-level ErrorBoundary in GustoProviderCustomUIAdapter. These are emitted to observability.onError with a componentStack for debugging.
The BaseLayout component renders errors based on their shape:
- Field errors — displayed as an unordered list of individual field messages
- Validation errors — displayed as preformatted Zod output
- All other errors — displayed as a generic "An error occurred" message
To track and monitor errors in production, use the observability hooks. These allow you to send errors to services like Sentry, Datadog, or your own monitoring solution.
See the Observability documentation for detailed information on error tracking, PII sanitization, and integration examples.