Skip to content

Commit

Permalink
Added typing to tryHandlers to catch static errors
Browse files Browse the repository at this point in the history
  • Loading branch information
juskek committed Feb 28, 2023
1 parent e829788 commit bbb415b
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 10 additions & 8 deletions lib/src/try_handler/async_try_handler.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic> asyncTryHandler({
required Future<dynamic> Function() tryFunction,
Map<dynamic, Future<dynamic> Function(Object e)>? catchKnownExceptions,
Future<dynamic> Function(Object e)? catchUnknownExceptions,
Future<T> asyncTryHandler<T>({
required Future<T> Function() tryFunction,
Map<dynamic, Future<T> Function(Object e)>? catchKnownExceptions,
Future<T> Function(Object e)? catchUnknownExceptions,
void Function()? finallyFunction,
}) async {
//! Validate Catch Known
Expand Down Expand Up @@ -38,10 +37,13 @@ Future<dynamic> 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();
Expand Down
13 changes: 8 additions & 5 deletions lib/src/try_handler/sync_try_handler.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic, dynamic Function(Object e)>? catchKnownExceptions,
dynamic Function(Object e)? catchUnknownExceptions,
T syncTryHandler<T>({
required T Function() tryFunction,
Map<dynamic, T Function(Object e)>? catchKnownExceptions,
T Function(Object e)? catchUnknownExceptions,
void Function()? finallyFunction,
}) {
//! Validate Catch Known
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
46 changes: 2 additions & 44 deletions test/flutter_dev_utils_test.dart
Original file line number Diff line number Diff line change
@@ -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();
}
72 changes: 72 additions & 0 deletions test/try_handler/test_try_handler.dart
Original file line number Diff line number Diff line change
@@ -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<bool>(tryFunction: () {
return true;
});

expect(result, true);
});

test('catch_static_return_type_async', () async {
final result = await asyncTryHandler<bool>(tryFunction: () async {
return true;
});

expectLater(result, true);
});
});
}

0 comments on commit bbb415b

Please sign in to comment.