anyhow 3.0.1 copy "anyhow: ^3.0.1" to clipboard
anyhow: ^3.0.1 copied to clipboard

Idiomatic error handling capabilities to make your code safer, more maintainable, and errors easier to debug. Dart implementation of Rust's Result monad type and "anyhow" crate.

example/main.dart

import 'package:anyhow/anyhow.dart';

void main() {
  //Error.stackTraceDisplayFormat = StackTraceDisplayFormat.full;
  print(order("Bob", 1));
}

Result<String> order(String user, int orderNumber) {
  final result =
      makeFood(orderNumber).context("Could not order for user: $user.");
  if (result case Ok(v: final order)) {
    return Ok("Order of $order is complete for $user");
  }
  return result;
}

Result<String> makeFood(int orderNumber) {
  if (orderNumber == 1) {
    return makeHamburger().context("Order number $orderNumber failed.");
  }
  return Ok("pasta");
}

Result<String> makeHamburger() {
  return bail("Hmm something went wrong making the hamburger.");
}

//Output:
// Error: Could not order for user: Bob.
//
// Caused By:
//    0: Order number 1 failed.
//    1: Hmm something went wrong making the hamburger.
//
// StackTrace:
// #0      AnyhowResultExtensions.context (package:anyhow/src/anyhow/anyhow_extensions.dart:12:29)
// #1      order (package:anyhow/test/src/temp.dart:9:40)
// #2      main (package:anyhow/example/main.dart:5:9)
// ... <OMITTED FOR EXAMPLE>
copied to clipboard
31
likes
160
points
356
downloads

Publisher

verified publishervoyver.com

Weekly Downloads

2024.09.13 - 2025.03.28

Idiomatic error handling capabilities to make your code safer, more maintainable, and errors easier to debug. Dart implementation of Rust's Result monad type and "anyhow" crate.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

rust

More

Packages that depend on anyhow