Codenic Clean Architecture
  • Introduction
  • The Clean Architecture
    • Presentation Layer
    • Domain Layer
    • Infrastructure Layer
  • Tutorial
    • Overview
    • Creating a Flutter Modular Project
      • Tips for Managing a Modular Project
    • Implementing the Domain Layer
      • Core Dependencies
      • Creating an Entity
      • Creating a Failure
      • Creating a Repository Interface
      • Creating Use Cases
        • CRUD Operations (Runner)
        • Data Streams (Watcher)
    • Implementing the Infrastructure Layer
      • External Dependencies
      • Creating a Data Model
      • Creating a Data Source
      • Implementing a Repository
    • Implementing the Presentation Layer
      • External Dependencies
      • Dependency Injection and Service Locator
      • Widgets
        • Snackbar Handler
        • Global Blocs Widget
        • Note Widgets
  • Packages
    • Codenic Bloc Use Case
      • Runner
      • Watcher
    • Codenic Logger
      • Usage
      • Example
      • Modifying the Logger
      • Integrating Firebase Crashlytics to the logger
    • Codenic Exception Converter
      • Failure Object
      • Exception Converter
      • Exception Converter Suite
      • Example
Powered by GitBook
On this page
  1. Packages
  2. Codenic Exception Converter

Exception Converter

PreviousFailure ObjectNextException Converter Suite

Last updated 2 years ago

The ExceptionConverter class allows you to map a specific Exception to a particular object. While a single ExceptionConverter may not be very useful on its own, you can use a group of exception converters together in an to run a task and automatically convert any exceptions that are thrown into the appropriate Failure object.

Exception converter is integrated with the to provide logging capabilities to the executable task.

To create an Exception Converter, create a class that extends the ExceptionConverter class:

/// A custom exception converter for converting a [SocketException] to a
/// [NetworkFailure] if an error occurs.
///
/// If no exception is caught, then [T] is returned.
class SocketExceptionConverter<T>
    extends ExceptionConverter<SocketException, T> {
  const SocketExceptionConverter();

  @override
  Failure convert({
    required SocketException exception,
    StackTrace? stackTrace,
    CodenicLogger? logger,
    MessageLog? messageLog,
  }) {
    if (logger != null && messageLog != null) {
      // Optional: you can log the exception here if needed
      logger.wtf(messageLog, error: exception, stackTrace: stackTrace);
    }

    return const NetworkFailure();
  }
}

Failure
ExceptionConverterSuite
Codenic Logger