thiserror 0.2.1 copy "thiserror: ^0.2.1" to clipboard
thiserror: ^0.2.1 copied to clipboard

Error Enum handling library for Dart.

thiserror #

Pub Version Dart Package Docs License: MIT Build Status

thiserror is a library for conveniently writing Error enums (sealed classes) in Dart and is based off the Rust crate with the same name thiserror.

ThisError #

Extending the ThisError class allows you to define errors with a string representation computed at display time.

How To Use #

ThisError objects can be passed a template and objects to inject into that template at the corresponding index if toString() is called. Missing template data will never cause an issue.

Here is an example of modeling an IoError with the rust_core Result type.

sealed class IoError extends ThisError<IoError> {
  const IoError([super.template, super.values]);
}

final class IoErrorDiskRead extends IoError {
  IoErrorDiskRead(String path) : super("Could not read '{0}' from disk.", [path]);
}

final class IoErrorDiskWrite extends IoError {
  Object obj;

  IoErrorDiskWrite(this.obj, String path) : super("Could not write '{0}' to '{1}' on disk.", [obj, path]);
}

final class IoErrorUnknown extends IoError {
  IoErrorUnknown() : super("An unknown error occurred.");
}

final class IoErrorEmpty extends IoError {
  IoErrorEmpty();
}

Result<(), IoError> writeToDisk(Object objToWrite) {
  final diskpath = "/home/user/data_file";
  // write fails..
  final ioError = IoErrorDiskWrite(objToWrite, diskpath);
  return Err(ioError);
}

void main() {
  final result = writeToDisk("data here");
  if(result case Err(:final err)){
    switch (err) {
        case IoErrorDiskRead():
        // your code here
        case IoErrorDiskWrite(:final obj):
        // your code here
        case IoErrorUnknown():
        // your code here
        case IoErrorEmpty():
        // your code here
    }
  }
  print(result);
}

Output:

IoError: Could not write 'data here' to '/home/user/data_file' on disk.

Comparison to anyhow #

Use thiserror if you care about designing your own dedicated error type(s) so that the caller receives exactly the information that you choose in the event of failure. This most often applies to library-like code. Use anyhow if you don't care what error type your functions return, you just want it to be easy. This is common in application-like code.