Angular Error Tracking Service
This challenge focuses on building a robust error tracking mechanism within an Angular application. Effectively tracking errors is crucial for maintaining application stability, identifying bugs, and providing a seamless user experience. You will create a service that intercepts and logs errors, allowing for centralized management and reporting.
Problem Description
Your task is to implement an ErrorTrackingService in TypeScript for an Angular application. This service should intercept unhandled JavaScript errors and Angular exceptions. It should then log these errors in a structured format to the browser's console for development purposes. For a production environment, you'll add functionality to optionally send these errors to a remote error tracking service (simulated in this challenge).
Key Requirements:
- Global Error Handling: Implement a mechanism to catch both uncaught JavaScript errors and Angular's
ErrorHandler. - Error Formatting: Log errors with essential context, including the error message, stack trace, component name (if available), and the timestamp.
- Console Logging: All intercepted errors must be logged to the browser's
console.error. - Production Mode Simulation: Implement a method to conditionally send errors to a simulated remote error tracking endpoint. This should only occur when the application is in a "production" state.
- Service Interface: Design the
ErrorTrackingServiceto be injectable and easily configurable.
Expected Behavior:
- When an error occurs anywhere in the application, the
ErrorTrackingServiceshould intercept it. - The error details (message, stack trace, timestamp, etc.) should be logged to the console.
- If the application is in production mode, the error details should be sent to a simulated remote service.
Edge Cases to Consider:
- Errors occurring during service initialization.
- Handling different types of errors (e.g.,
Errorobjects, plain strings). - Ensuring the error tracking mechanism itself doesn't throw new errors.
Examples
Example 1: Uncaught JavaScript Error
// Simulate an uncaught error in the application
throw new Error("Something unexpected happened!");
Expected Console Output (during development):
[Timestamp] Error Type: Error
[Timestamp] Message: Something unexpected happened!
[Timestamp] Stack Trace: (Actual stack trace details)
[Timestamp] Component: (If discernible, otherwise null/undefined)
Expected Behavior (in production): The ErrorTrackingService would call a hypothetical remoteErrorService.logError(...) with the formatted error details.
Example 2: Angular Component Error
// In a component
@Component({ ... })
export class MyComponent implements OnInit {
ngOnInit() {
throw new Error("Error in ngOnInit");
}
}
Expected Console Output (during development):
[Timestamp] Error Type: Error
[Timestamp] Message: Error in ngOnInit
[Timestamp] Stack Trace: (Actual stack trace details, possibly including MyComponent)
[Timestamp] Component: MyComponent
Expected Behavior (in production): The ErrorTrackingService would call a hypothetical remoteErrorService.logError(...) with the formatted error details, including MyComponent as the component.
Constraints
- The
ErrorTrackingServicemust be implemented in TypeScript. - You should leverage Angular's dependency injection system.
- Simulate the remote error tracking endpoint using a simple
console.logwithin a dedicated method in your service. - The "production mode" simulation can be a simple boolean flag, e.g.,
isProduction: boolean.
Notes
- Consider using
window.onerrorand Angular'sErrorHandlerto catch global errors. - Think about how to extract the component name from the stack trace, though this can be challenging and might require specific patterns or libraries. For this challenge, a best-effort approach is acceptable.
- The goal is to build a foundational error tracking system. For real-world applications, you would integrate with services like Sentry, Bugsnag, or Application Insights.
- You'll need to set up an
AppModuleor a dedicated error module to provide theErrorTrackingServiceand configure the global error handler.