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

A library for concisely defining error types (error enums / sealed classes) and their String representation.

example/main.dart

// ignore_for_file: unused_local_variable

import 'package:thiserror/thiserror.dart';

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

final class IoErrorDiskRead extends IoError {
  IoErrorDiskRead(String path)
      : super(() => "Could not read '$path' from disk.");
}

final class IoErrorDiskWrite extends IoError {
  Object obj;

  IoErrorDiskWrite(this.obj, String path)
      : super(() => "Could not write '$obj' to '$path' on disk.");
}

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

final class IoErrorEmpty extends IoError {
  const IoErrorEmpty();
}

// Usually, you would use a result type like from the `rust_core` package.
IoError writeToDisk(Object objToWrite) {
  final diskpath = "/home/user/data_file";
  // write fails..
  final ioError = IoErrorDiskWrite(objToWrite, diskpath);
  return ioError;
}

void main() {
  final err = writeToDisk("data here");
  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(err);
  // IoError: Could not write 'data here' to '/home/user/data_file' on disk.
}
2
likes
160
pub points
0%
popularity

Publisher

verified publishervoyver.com

A library for concisely defining error types (error enums / sealed classes) and their String representation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on thiserror