Error Handling Patterns
Comprehensive error handling patterns for building resilient Aurelia applications with graceful degradation and user-friendly feedback.
1. Global error boundary with user feedback
Steps
import { ILogger, resolve } from '@aurelia/kernel'; import { observable } from '@aurelia/runtime'; export interface AppError { message: string; stack?: string; timestamp: number; context?: string; } export class ErrorBoundaryService { @observable currentError: AppError | null = null; private errors: AppError[] = []; private logger = resolve(ILogger); captureError(error: Error, context?: string) { const appError: AppError = { message: error.message, stack: error.stack, timestamp: Date.now(), context }; this.errors.push(appError); this.currentError = appError; this.logger.error(`[${context || 'Unknown'}] ${error.message}`, error); return appError; } clearError() { this.currentError = null; } getRecentErrors(count: number = 10) { return this.errors.slice(-count); } }import { Aurelia } from 'aurelia'; import { ErrorBoundaryService } from './error-boundary-service'; export async function main() { const au = Aurelia.app(MyApp); const errorBoundary = au.container.get(ErrorBoundaryService); window.addEventListener('error', (event) => { errorBoundary.captureError(event.error, 'Window'); event.preventDefault(); }); window.addEventListener('unhandledrejection', (event) => { errorBoundary.captureError( new Error(event.reason?.message || 'Unhandled promise rejection'), 'Promise' ); event.preventDefault(); }); await au.start(); }import { resolve } from '@aurelia/kernel'; import { ErrorBoundaryService } from './error-boundary-service'; export class MyApp { private errorBoundary = resolve(ErrorBoundaryService); reload() { window.location.reload(); } dismissError() { this.errorBoundary.clearError(); } }<div class="error-banner" if.bind="errorBoundary.currentError"> <div class="error-content"> <h3>Something went wrong</h3> <p>${errorBoundary.currentError.message}</p> <div class="error-actions"> <button click.trigger="dismissError()">Dismiss</button> <button click.trigger="reload()">Reload Page</button> </div> </div> </div> <au-viewport></au-viewport>
Checklist
2. API error handling with retry and fallback
Steps
Checklist
3. Router navigation error handling
Steps
Checklist
4. Form validation errors with field-level feedback
Steps
Checklist
5. Async operation error boundaries with timeout
Steps
Checklist
6. Optimistic updates with rollback on error
Steps
Checklist
Error handling cheat sheet
Scenario
Pattern
Key Components
Best practices
See also
Last updated
Was this helpful?