package_exception_handler 0.2.3 package_exception_handler: ^0.2.3 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);
}
}
}
To dispose this mixin correctly use disposeErrorStream
method like that:
class SomePackageClass with ErrorStream {
void dispose() {
disposeErrorStream();
}
}
To re init this mixin use initErrorStream
method within class ctor or within init method like that:
// Within class ctor
class SomePackageClass with ErrorStream {
SomePackageClass() {
initErrorStream();
}
}
// OR within init method
class SomeWidgetState extends State<SomeWidget> with ErrorStream {
@override
initState() {
initErrorStream();
super.initState();
}
}
TODO #
- Add tests for mixin methods