Skip to content

Implement Custom Exceptions for Consistent Error Handling in Data Access Layer #68

Open
@0010aor

Description

@0010aor

Description

Refactor the Data Access Layer (DAL) and repository code—especially in files like cards.ts, collections.ts, and practiceSessions.ts in frontend/src/data/localDB/—to use custom exception classes instead of generic throw new Error(...) statements. This will ensure that errors such as "not found," "validation," or "database" errors are clearly distinguished and can be handled appropriately at higher layers (services, UI).

Current Behavior

  • The DAL and repository files (e.g., cards.ts) throw generic errors, such as:
    if (!card) throw new Error('Card not found for update')
  • All error cases are represented by the base Error class, making it difficult to distinguish between different error types (e.g., not found vs. validation error).
  • Higher layers (services, UI) cannot easily provide specific feedback or handling based on error type.

Expected Behavior

  • Define custom error classes (e.g., NotFoundError, ValidationError, DatabaseError) in a shared location
  • Replace all throw new Error(...) statements in the DAL/repository files with the appropriate custom error class.
    • For example, in cards.ts:
      if (!card) throw new NotFoundError('Card not found for update')
  • Update similar cases in collections.ts and practiceSessions.ts (e.g., when a collection or session is not found, or when validation fails).
  • Higher layers (services, UI) should catch and handle these custom exceptions to provide more specific user feedback and logging.
  • Documentation is updated to describe the new exception classes and their intended usage.

Examples of Cases to Update

  • Throwing when a card, collection, or session is not found (e.g., Card not found for update, Collection not found).
  • Throwing when validation fails (e.g., invalid input data).
  • Throwing when a database operation fails (e.g., IndexedDB errors).

Metadata

Metadata

Assignees

No one assigned

    Labels

    frontendonly frontend issue

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions