codenic_exception_converter 1.0.2 codenic_exception_converter: ^1.0.2 copied to clipboard
A utility tool for converting Exception into Failure objects to make your code more portable and easier to maintain.
// 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'),
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()],
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(
error: 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.
class SocketExceptionConverter<T>
extends ExceptionConverter<SocketException, NetworkFailure, T> {
const SocketExceptionConverter();
@override
NetworkFailure 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.error(messageLog, error: exception, stackTrace: stackTrace);
}
return const NetworkFailure();
}
}