codenic_exception_converter 5.0.0 copy "codenic_exception_converter: ^5.0.0" to clipboard
codenic_exception_converter: ^5.0.0 copied to clipboard

A utility tool for converting Exception into Failure objects to make your code more portable and easier to maintain.

example/main.dart

// ignore_for_file: avoid_print

import 'dart:io';

import 'package:codenic_exception_converter/codenic_exception_converter.dart';

Future<void> main() async {
  // To run, type `dart --enable-asserts example/main.dart`.

  await _observeWithDefaultConverters();
  print('');
  await _observeWithArgConverters();
  print('');
  await _observeNoExceptionConverters();
  print('');
  _convert();
}

Future<void> _observeWithDefaultConverters() async {
  // Create an exception converter suite that can convert a `SocketException`
  // into a `NetworkFailure`
  final exceptionConverterSuite = ExceptionConverterSuite(
    exceptionConverters: [SocketExceptionConverter.new],
  );

  final result = await exceptionConverterSuite.observe<void>(
    messageLog: MessageLog(id: 'observe-with-default-converters'),
    printOutput: true,
    task: (messageLog) {
      // Simulate exception
      throw const SocketException('test');
    },
  );

  print('Observe (with default converters): $result');
}

Future<void> _observeWithArgConverters() async {
  final exceptionConverterSuite = ExceptionConverterSuite();

  final result = await exceptionConverterSuite.observe<void>(
    messageLog: MessageLog(id: 'observe-with-argument-converters'),
    // Provide an exception converter as an argument
    exceptionConverters: [const SocketExceptionConverter()],
    printOutput: true,
    task: (messageLog) {
      // Simulate exception
      throw const SocketException('test');
    },
  );

  print('Observe (with argument exception converters): $result');
}

Future<void> _observeNoExceptionConverters() async {
  final exceptionConverterSuite = ExceptionConverterSuite();

  final result = await exceptionConverterSuite.observe<void>(
    messageLog: MessageLog(id: 'observe-with-no-exception-converters'),
    task: (messageLog) {
      try {
        // Simulate exception
        throw const SocketException('test');
      } on SocketException {
        return const Left(NetworkFailure());
      }
    },
  );

  print('Observe (no exception converter): $result');
}

void _convert() {
  final exceptionConverterSuite = ExceptionConverterSuite(
    exceptionConverters: [SocketExceptionConverter.new],
  );

  final result = exceptionConverterSuite.convert(
    exception: const SocketException('test'),
    exceptionConverters: [const SocketExceptionConverter()],
  );

  print('Convert result: $result');
}

class NetworkFailure extends Failure {
  const NetworkFailure({super.message = 'A network failure occurred'});
}

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

  @override
  Failure convert(SocketException exception) => const NetworkFailure();

  @override
  void logException(
    SocketException exception,
    StackTrace stackTrace,
    CodenicLogger logger,
    MessageLog messageLog,
  ) =>
      logger.error(messageLog, error: exception, stackTrace: stackTrace);
}
2
likes
0
pub points
35%
popularity

Publisher

verified publishercodenic.dev

A utility tool for converting Exception into Failure objects to make your code more portable and easier to maintain.

Homepage
Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

codenic_logger, equatable, fpdart

More

Packages that depend on codenic_exception_converter