df_safer_dart 0.10.0
df_safer_dart: ^0.10.0 copied to clipboard
A package inspired by functional programming, designed to enhance the structure, safety, and debuggability of mission-critical code.
// Example:
//
// Using Resolvable and Result for safer error handling without try-catch
// blocks.
//
// Explicit error handling is enforced, providing compile-time safety.
import 'package:df_safer_dart/df_safer_dart.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() async {
// Fetch the IP address and handle both success and error results.
fetchIpAddress().flatMap(
(result) => result
.ifOk((e) {
print('IP address: ${result.unwrap()}');
})
.ifErr((e) {
print('Error: $e');
}),
);
}
Async<String> fetchIpAddress() {
// Async, Sync or Resolvable can be used to wrap
// potentially throwing code.
//
// The only rules here are:
//
// 1. ALWAYS await all asynchronous operations inside Async
// (or Resolvable) to ensure that exceptions are properly caught and
// wrapped in a Result.
//
// 2. Only deal with asynchronous operations in Async or
// Resolvable. Not in Sync.
//
// 3. You can throw any Objects within unsafe, but prefer throwing Err
// objects as it is the standard and will help with debugging.
return Async(() async {
final response = await http.get(
Uri.parse('https://api.ipify.org?format=json'),
);
// Throw an Err if the status code is not 200. Any other exceptions within
// Resolvable.wrap will be caught and wrapped in an Err.
if (response.statusCode != 200) {
throw Err(
// The debugPath will be printed when the error is thrown.
debugPath: ['fetchIpAddress'],
error: 'Failed to fetch IP address',
);
}
final data = jsonDecode(response.body);
final ip = data['ip'] as String;
return ip;
});
}
copied to clipboard