package_exception_handler 0.0.1
package_exception_handler: ^0.0.1 copied to clipboard
Flutter package for exception handling within custom packages.
package_exception_handler #
Flutter package for exception handling within custom packages.
Getting Started #
Withing your custom package use ErrorStream mixin to get access to error stream and recordError function for method that must return value, if return type is void, handleError or handleErrorAsync function can be used to auto handle an error or exception.
error stream is of type PackageException. To use it within your app subscribe to error stream and get Exception or Error like that:
pluginOrPackage.error.listen(
(PackageException pkgExc) {
var current = pkgExc.currentException; // Get current exception
var exc = pkgExc.innerException; // Get an exception directly
var error = pkgExc.innerError; // Get an error directly
var stackTrace = pkgExc.stackTrace; // Get StackTrace if it have been provided
// do something with error or exception
}
);
Examples #
if return type is void and function is synchronous
class SomePackageClass with ErrorStream {
void someFunc() => handleError(() {
// do something
});
}
if return type is void and function is asynchronous
class SomePackageClass with ErrorStream {
void someFunc() => handleErrorAsync(() async {
// do something
});
}
if return type isn't void and function is synchronous
class SomePackageClass with ErrorStream {
bool someFunc() {
try {
// do something
}
catch (e, stackTrace) {
recordError(e, stackTrace);
return false;
}
}
}
if return type isn't void and function is asynchronous
class SomePackageClass with ErrorStream {
Future<bool> someFunc() async {
try {
// do something
}
catch (e, stackTrace) {
recordError(e, stackTrace);
return Future.value(false);
}
}
}