diff --git a/CHANGELOG.md b/CHANGELOG.md index 9990e6e..53b093f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ +## 0.0.3 + +* Added typing to tryHandlers to catch static errors + ## 0.0.2+1 * Fixed catchKnownExceptions + ## 0.0.2 * Modified TypeFilter to extend ProductionFilter instead of DevelopmentFilter so that the stackTrace is always printed. diff --git a/lib/src/try_handler/async_try_handler.dart b/lib/src/try_handler/async_try_handler.dart index 51c2983..e6d2413 100644 --- a/lib/src/try_handler/async_try_handler.dart +++ b/lib/src/try_handler/async_try_handler.dart @@ -1,13 +1,12 @@ import 'package:flutter_dev_utils/src/try_handler/catch_utils.dart'; -import 'package:flutter_dev_utils/src/try_handler/logger.dart'; /// Asynchronous try and catch handler to reduce boilerplate /// /// Should be called in a State file -Future asyncTryHandler({ - required Future Function() tryFunction, - Map Function(Object e)>? catchKnownExceptions, - Future Function(Object e)? catchUnknownExceptions, +Future asyncTryHandler({ + required Future Function() tryFunction, + Map Function(Object e)>? catchKnownExceptions, + Future Function(Object e)? catchUnknownExceptions, void Function()? finallyFunction, }) async { //! Validate Catch Known @@ -38,10 +37,13 @@ Future asyncTryHandler({ } } } - } catch (e, s) { + } catch (e) { //! Handle Unknown Errors and Exceptions - utilsLogger.e('Caught unknown exception', e, s); - return await catchUnknownExceptions?.call(e); + if (catchUnknownExceptions == null) { + rethrow; + } + + return await catchUnknownExceptions.call(e); } finally { //! Finally finallyFunction?.call(); diff --git a/lib/src/try_handler/sync_try_handler.dart b/lib/src/try_handler/sync_try_handler.dart index 987fa9e..578a1ee 100644 --- a/lib/src/try_handler/sync_try_handler.dart +++ b/lib/src/try_handler/sync_try_handler.dart @@ -1,10 +1,10 @@ import 'package:flutter_dev_utils/src/try_handler/catch_utils.dart'; /// Synchronous try and catch handler to reduce boilerplate -dynamic syncTryHandler({ - required dynamic Function() tryFunction, - Map? catchKnownExceptions, - dynamic Function(Object e)? catchUnknownExceptions, +T syncTryHandler({ + required T Function() tryFunction, + Map? catchKnownExceptions, + T Function(Object e)? catchUnknownExceptions, void Function()? finallyFunction, }) { //! Validate Catch Known @@ -37,7 +37,10 @@ dynamic syncTryHandler({ } } catch (e) { //! Handle Unknown Errors and Exceptions - return catchUnknownExceptions?.call(e); + if (catchUnknownExceptions == null) { + rethrow; + } + return catchUnknownExceptions.call(e); } finally { //! Finally finallyFunction?.call(); diff --git a/pubspec.yaml b/pubspec.yaml index a6df03e..5858e67 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_dev_utils description: Developer utils to make your life easier; e.g. try handlers to easily find source of bug, help classes for code editor inline tips. -version: 0.0.2+1 +version: 0.0.3 homepage: https://github.com/Kek-Tech/flutter_dev_utils # publish_to: none # Remove this line if you wish to publish to pub.dev diff --git a/test/flutter_dev_utils_test.dart b/test/flutter_dev_utils_test.dart index 404c6d1..ab3926f 100644 --- a/test/flutter_dev_utils_test.dart +++ b/test/flutter_dev_utils_test.dart @@ -1,47 +1,5 @@ -import 'package:flutter_dev_utils/flutter_dev_utils.dart'; -import 'package:flutter_dev_utils/src/test/test_exception.dart'; -import 'package:flutter_test/flutter_test.dart'; +import 'try_handler/test_try_handler.dart'; void main() { - test('test_async_try_handler', () async { - final result = await asyncTryHandler( - tryFunction: () async { - return await Future.delayed( - const Duration(milliseconds: 50), () => true); - }, - ); - expectLater(result, true); - }); - - test('catch_correct_known_exception_async', () async { - final result = await asyncTryHandler( - tryFunction: () async { - return await Future.delayed(const Duration(milliseconds: 50), () { - throw TestException(); - }); - }, - catchKnownExceptions: { - TestException(): (e) async => true, - }, - catchUnknownExceptions: (e) async => false, - ); - - expectLater(result, true); - }); - test('catch_correct_known_exception_sync', () { - final result = syncTryHandler( - tryFunction: () { - throw TestException(); - }, - catchKnownExceptions: { - TestException(): (e) => true, - }, - catchUnknownExceptions: (e) => false, - ); - - expectLater(result, true); - }); - test('test_sync_try_handler', () { - expect('a', 'a'); - }); + testTryHandler(); } diff --git a/test/try_handler/test_try_handler.dart b/test/try_handler/test_try_handler.dart new file mode 100644 index 0000000..cdbeea0 --- /dev/null +++ b/test/try_handler/test_try_handler.dart @@ -0,0 +1,72 @@ +import 'package:flutter_dev_utils/flutter_dev_utils.dart'; +import 'package:flutter_dev_utils/src/test/test_exception.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void testTryHandler() { + group('try handler', () { + test('test_sync_try_handler', () { + final result = syncTryHandler( + tryFunction: () { + return true; + }, + ); + expect(result, true); + }); + + test('test_async_try_handler', () async { + final result = await asyncTryHandler( + tryFunction: () async { + return await Future.delayed( + const Duration(milliseconds: 50), () => true); + }, + ); + expectLater(result, true); + }); + + test('catch_correct_known_exception_sync', () { + final result = syncTryHandler( + tryFunction: () { + throw TestException(); + }, + catchKnownExceptions: { + TestException(): (e) => true, + }, + catchUnknownExceptions: (e) => false, + ); + + expect(result, true); + }); + + test('catch_correct_known_exception_async', () async { + final result = await asyncTryHandler( + tryFunction: () async { + return await Future.delayed(const Duration(milliseconds: 50), () { + throw TestException(); + }); + }, + catchKnownExceptions: { + TestException(): (e) async => true, + }, + catchUnknownExceptions: (e) async => false, + ); + + expectLater(result, true); + }); + + test('catch_static_return_type_sync', () { + final result = syncTryHandler(tryFunction: () { + return true; + }); + + expect(result, true); + }); + + test('catch_static_return_type_async', () async { + final result = await asyncTryHandler(tryFunction: () async { + return true; + }); + + expectLater(result, true); + }); + }); +}