package_exception_handler 0.2.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 58

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

Contributors #

Changelog #

[0.2.2]

  • Moved repo to eo.march.plugins
  • Minor fixes

[0.2.1]

  • Fixed example readme file

[0.2.0]

  • Added example project

[0.1.2]

  • Fixed minor issues

[0.1.1]

  • Added to ErrorStream mixin initErrorStream method
  • Added documentation for public APIs
  • Added analysis_options.yaml

[0.1.0]

  • Added to ErrorStream mixin disposeErrorStream method

[0.0.1]

  • Added PackageException
  • Added ErrorStream mixin

example/README.md

package_exception_handler_example #

Demonstrates how to use the package_exception_handler plugin.

Usage #

class MyApp extends StatefulWidget {
  final navigatorKey = GlobalKey<NavigatorState>();
  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with ErrorStream {
  var stackTrace = '';

  @override
  void initState() {
    initErrorStream(); // To be sure that after disposing Stream will be initialized again
    error.listen((data) {
      widget.scaffoldKey.currentState.showSnackBar(SnackBar(
        duration: const Duration(seconds: 3),
        content: Text(data.currentException.toString()), // Gets current Error or Exception
        action: SnackBarAction(
          label: 'Stack Trace',
          onPressed: () => setState(() => stackTrace = data.stackTrace
              ?.toString()), // Gets stack trace, if it have been provided
        ),
      ));
    });
    super.initState();
  }

  @override
  void dispose() {
    disposeErrorStream(); // Need to dispose error to prevent memory leaks
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: widget.navigatorKey,
      home: Scaffold(
        key: widget.scaffoldKey,
        appBar: AppBar(
          title: const Text('Pkg Exception Handler Example App'),
        ),
        body: Center(
          child: FractionallySizedBox(
            widthFactor: 0.75,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  onPressed: () {
                    // Handles sync error if retutn type is void or it doesn't matter
                    handleError(() {
                      // do stuff that might fail
                      throw AssertionError();
                    });
                  },
                  child: Text('Generate Error'),
                ),
                SizedBox(height: 25),
                RaisedButton(
                  onPressed: () {
                    // Handles async error if retutn type is void or it doesn't matter
                    handleErrorAsync(Future.delayed(const Duration(seconds: 2))
                        .whenComplete(() => throw FormatException()));
                  },
                  child: Text('Generate Async Error'),
                ),
                SizedBox(height: 25),
                RaisedButton(
                  onPressed: () {
                    // Handle error on your own using recordError method within catch statement
                    try {
                      // do stuff that might fail
                      throw UnsupportedError('test error');
                    } catch (e, stackTrace) {
                      recordError(e,
                          stackTrace); // Add Error or Exception (with StackTrace) to stream
                      return false;
                    }
                  },
                  child: Text('Generate Error with Raw Handling'),
                ),
                SizedBox(height: 50),
                InkResponse(
                  onTap: () => setState(() => stackTrace = ''),
                  child: Text(stackTrace),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Getting Started #

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  package_exception_handler: ^0.2.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:package_exception_handler/package_exception_handler.dart';
  
Version Uploaded Documentation Archive
0.2.2 May 26, 2019 Go to the documentation of package_exception_handler 0.2.2 Download package_exception_handler 0.2.2 archive
0.2.1 Mar 6, 2019 Go to the documentation of package_exception_handler 0.2.1 Download package_exception_handler 0.2.1 archive
0.2.0 Mar 5, 2019 Go to the documentation of package_exception_handler 0.2.0 Download package_exception_handler 0.2.0 archive
0.1.2 Mar 5, 2019 Go to the documentation of package_exception_handler 0.1.2 Download package_exception_handler 0.1.2 archive
0.1.1 Mar 4, 2019 Go to the documentation of package_exception_handler 0.1.1 Download package_exception_handler 0.1.1 archive
0.1.0 Mar 4, 2019 Go to the documentation of package_exception_handler 0.1.0 Download package_exception_handler 0.1.0 archive
0.0.1 Mar 3, 2019 Go to the documentation of package_exception_handler 0.0.1 Download package_exception_handler 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
18
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
58
Learn more about scoring.

We analyzed this package on Jun 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.2
  • pana: 0.12.18
  • Flutter: 1.5.4-hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance issues and suggestions

Support latest dependencies. (-5 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
pedantic ^1.5.0 1.7.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test