Skip to content

Commit bd7dfb7

Browse files
[camera_platform_interface] Fix asynchronous exceptions handling of the initializeCamera method (flutter#4660)
1 parent cd97c19 commit bd7dfb7

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

packages/camera/camera_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.1.5
2+
3+
* Fixes asynchronous exceptions handling of the `initializeCamera` method.
4+
15
## 2.1.4
26

37
* Removes dependency on `meta`.

packages/camera/camera_platform_interface/lib/src/method_channel/method_channel_camera.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ class MethodChannelCamera extends CameraPlatform {
126126
'cameraId': cameraId,
127127
'imageFormatGroup': imageFormatGroup.name(),
128128
},
129+
).catchError(
130+
(Object error, StackTrace stackTrace) {
131+
if (error is! PlatformException) {
132+
throw error;
133+
}
134+
_completer.completeError(
135+
CameraException(error.code, error.message),
136+
stackTrace,
137+
);
138+
},
129139
);
130140

131141
return _completer.future;

packages/camera/camera_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.1.4
7+
version: 2.1.5
88

99
environment:
1010
sdk: '>=2.12.0 <3.0.0'

packages/camera/camera_platform_interface/test/method_channel/method_channel_camera_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,38 @@ void main() {
126126
);
127127
});
128128

129+
test(
130+
'Should throw CameraException when initialize throws a PlatformException',
131+
() {
132+
// Arrange
133+
MethodChannelMock(
134+
channelName: 'plugins.flutter.io/camera',
135+
methods: <String, dynamic>{
136+
'initialize': PlatformException(
137+
code: 'TESTING_ERROR_CODE',
138+
message: 'Mock error message used during testing.',
139+
)
140+
},
141+
);
142+
final MethodChannelCamera camera = MethodChannelCamera();
143+
144+
// Act
145+
expect(
146+
() => camera.initializeCamera(0),
147+
throwsA(
148+
isA<CameraException>()
149+
.having((CameraException e) => e.code, 'code',
150+
'TESTING_ERROR_CODE')
151+
.having(
152+
(CameraException e) => e.description,
153+
'description',
154+
'Mock error message used during testing.',
155+
),
156+
),
157+
);
158+
},
159+
);
160+
129161
test('Should send initialization data', () async {
130162
// Arrange
131163
final MethodChannelMock cameraMockChannel = MethodChannelMock(

0 commit comments

Comments
 (0)