Hone logo
Hone
Problems

Robust Vue Error Handling with Vuex and a Custom Error Component

This challenge focuses on implementing robust error handling within a Vue.js application using TypeScript. You will learn to centralize error management, display user-friendly error messages, and clear errors gracefully, creating a more resilient and professional user experience.

Problem Description

Your task is to build a system for handling errors that occur within a Vue.js application. This system should:

  1. Centralize Error State: Store all active errors in a Vuex store to provide a single source of truth for error information.
  2. Display User-Friendly Errors: Create a dedicated Vue component that listens to the Vuex store and displays active errors to the user in a clear and actionable way.
  3. Clear Errors Programmatically: Implement mechanisms to clear specific errors or all errors from the store when they are resolved or no longer relevant.
  4. Integrate with API Calls: Simulate an API call that can either succeed or fail, triggering the error handling mechanism.

Key Requirements:

  • Vuex Store: Define a Vuex module to manage the errors state. This state should be an array of error objects, each containing at least an id (unique identifier) and a message.
  • Error Display Component: Create a global Vue component (e.g., ErrorDisplay.vue) that:
    • Accesses the errors from the Vuex store.
    • Renders each active error with its message.
    • Provides a button to dismiss/clear individual errors using their id.
    • If there are errors, displays a prominent visual indicator (e.g., a red banner at the top of the page).
  • Actions and Mutations: Implement Vuex actions and mutations to:
    • Add new errors to the store.
    • Remove specific errors by id.
    • Clear all errors from the store.
  • Error Simulation: Create a Vue component (e.g., ApiSimulator.vue) with a button that, when clicked, simulates an API call. This simulated call should have a high probability of failing and dispatching an error to the Vuex store.
  • TypeScript Integration: Ensure all Vue components, Vuex modules, and any utility functions are written in TypeScript.

Expected Behavior:

  • When the "Simulate API Error" button is clicked and the simulated API call fails, an error message should appear in the ErrorDisplay component.
  • Each error message should have a close button. Clicking it should remove that specific error from the display and the Vuex store.
  • The ErrorDisplay component should be visible only when there are active errors.
  • A mechanism to clear all errors (e.g., a "Dismiss All" button in ErrorDisplay or a separate global button) should exist and function correctly.

Edge Cases:

  • Handling multiple concurrent errors.
  • Ensuring error IDs are unique.
  • Graceful behavior when the API simulator is clicked rapidly.

Examples

Example 1: Simulating a single API error

Input: User clicks the "Simulate API Error" button in ApiSimulator.vue. The simulated API call returns an error.

Output:
- The ErrorDisplay component becomes visible.
- A red banner appears at the top of the application.
- The banner displays: "Network Error: Could not fetch data." (or a similar generated error message).
- A "close" icon next to the error message.

Explanation: The ApiSimulator dispatches an 'addError' action with a unique ID and the error message. The Vuex store updates, triggering the ErrorDisplay component to render the error.

Example 2: Displaying multiple errors

Input: User clicks the "Simulate API Error" button twice in quick succession, and both simulated calls fail.

Output:
- The ErrorDisplay component remains visible.
- The banner displays both error messages, each with its own close button.
  - "Network Error: Could not fetch data." [x]
  - "API Gateway Timeout." [x]

Explanation: Two separate error objects with unique IDs are added to the Vuex store, and the ErrorDisplay component correctly renders both.

Example 3: Clearing a single error

Input: In Example 2, the user clicks the close button next to "Network Error: Could not fetch data."

Output:
- The ErrorDisplay component remains visible.
- Only the second error message is displayed: "API Gateway Timeout." [x]

Explanation: The ErrorDisplay component dispatches a 'removeError' action with the ID of the cleared error. The Vuex store updates, and the ErrorDisplay re-renders with the remaining error.

Example 4: Clearing all errors

Input: In Example 2, the user clicks a "Dismiss All" button (or equivalent) within the ErrorDisplay component.

Output:
- The ErrorDisplay component becomes hidden.
- The red banner disappears.
- No error messages are displayed.

Explanation: A 'clearErrors' action is dispatched, emptying the errors array in the Vuex store. This causes the ErrorDisplay component to unmount or render nothing.

Constraints

  • Vue.js version: 3.x
  • Vuex version: 4.x
  • TypeScript version: 4.x or higher
  • All Vue components and Vuex modules must be written in TypeScript (.vue files with <script lang="ts">).
  • The error simulation should have a success rate of no more than 20% and a failure rate of at least 80% to effectively test error handling.
  • Error IDs must be unique across all active errors in the store. A simple timestamp or a UUID generator can be used.

Notes

  • Consider using a library like uuid for generating unique IDs, or implement a simple counter-based approach.
  • Think about how to structure your Vuex module for clarity and maintainability.
  • The visual styling of the error banner is up to you, but it should be clearly distinguishable from the rest of the application's content.
  • You might want to explore Vue's global error handler (app.config.errorHandler) for catching uncaught exceptions during development, though this challenge focuses on application-level error dispatching.
  • The "Simulate API Error" action could be implemented using setTimeout to mimic network latency.
Loading editor...
typescript