ErrorHandler<E> class

Handles Dio exceptions and converts them to FailureResponse.

Also exposes apiError for the type-safe ApiError hierarchy.

Type Parameters: E - Custom error data type (optional)

Example:

try {
  final response = await dio.get('/users');
} catch (error) {
  final handler = ErrorHandler<LoginError>.handle(
    error,
    parseError: (json) => LoginError.fromJson(json),
  );

  // Legacy
  print(handler.failure.message);

  // Type-safe
  switch (handler.apiError) {
    case ValidationError(:final fields) => showFieldErrors(fields),
    case UnauthorizedError()            => redirectToLogin(),
    case ServerError(:final statusCode) => showServerError(statusCode),
    _                                   => showGenericError(),
  }
}
Implemented types

Constructors

ErrorHandler.handle(dynamic error, {E parseError(Map<String, dynamic>)?})

Properties

apiError ApiError
Type-safe error representation – use this on new screens.
latefinal
failure FailureResponse<E>
latefinal
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited