Skip to content

Commit

Permalink
feat: Add throwExceptionHandler (#102)
Browse files Browse the repository at this point in the history
Add `throwExceptionHandler ` to allow us to reuse the same throw
exception method in the package, rather than call `throw Exception`
directly.
  • Loading branch information
littleGnAl authored Aug 13, 2024
1 parent 1a240fa commit 4ec2c67
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ jobs:
integration_test_ios:
name: ios integration test
if: ${{ !contains(github.event.pull_request.labels.*.name, 'ci:skip') }}
runs-on: macos-13
runs-on: macos-14
timeout-minutes: 60
steps:
- uses: actions/checkout@v1
Expand All @@ -264,7 +264,7 @@ jobs:
integration_test_ios_use_frameworks:
name: ios integration test with use_frameworks
if: ${{ !contains(github.event.pull_request.labels.*.name, 'ci:skip') }}
runs-on: macos-13
runs-on: macos-14
timeout-minutes: 60
steps:
- uses: actions/checkout@v1
Expand Down
10 changes: 10 additions & 0 deletions lib/src/iris_method_channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import 'package:iris_method_channel/src/platform/iris_method_channel_internal.da

// ignore_for_file: public_member_api_docs

@pragma('vm:prefer-inline')
ThrowExceptionHandler throwExceptionHandler = _defaultThrowExceptionHandler;
@pragma('vm:prefer-inline')
void _defaultThrowExceptionHandler({required int code, String? message}) {
throw Exception();
}

typedef ThrowExceptionHandler = void Function(
{required int code, String? message});

class IrisMethodChannel {
IrisMethodChannel(this._nativeBindingsProvider) {
_irisMethodChannelInternal =
Expand Down
28 changes: 28 additions & 0 deletions test/iris_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -867,4 +867,32 @@ void main() {
await irisMethodChannel.dispose();
},
);

test(
'throwExceptionHandler throw Exception by default',
() async {
final currentThrowExceptionHandler = throwExceptionHandler;

expect(() => throwExceptionHandler(code: 1, message: 'message'),
throwsA(isA<Exception>()));

throwExceptionHandler = currentThrowExceptionHandler;
},
);

test(
'Can override throwExceptionHandler',
() async {
final currentThrowExceptionHandler = throwExceptionHandler;

throwExceptionHandler = ({required int code, String? message}) {
throw StateError('message');
};

expect(() => throwExceptionHandler(code: 1, message: 'message'),
throwsA(isA<StateError>()));

throwExceptionHandler = currentThrowExceptionHandler;
},
);
}

0 comments on commit 4ec2c67

Please sign in to comment.