Skip to content

Commit b298abd

Browse files
committed
Add dedicated error type and helper method
1 parent ab494e3 commit b298abd

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Diff for: packages/sane/lib/src/exceptions.dart

+5
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,8 @@ final class SaneUnsupportedException extends SaneException {
186186
@override
187187
SANE_Status get _status => SANE_Status.STATUS_UNSUPPORTED;
188188
}
189+
190+
/// SANE has been exited or the device has been closed.
191+
final class SaneDisposedError extends StateError {
192+
SaneDisposedError() : super('SANE has been exited');
193+
}

Diff for: packages/sane/lib/src/sane.dart

+17-12
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Sane {
9191
Future<List<SaneDevice>> getDevices({
9292
required bool localOnly,
9393
}) {
94-
if (_exited) throw StateError('SANE has been exited');
94+
_checkIfExited();
9595

9696
final completer = Completer<List<SaneDevice>>();
9797

@@ -121,7 +121,7 @@ class Sane {
121121
}
122122

123123
Future<SaneHandle> open(String deviceName) {
124-
if (_exited) throw StateError('SANE has been exited');
124+
_checkIfExited();
125125

126126
final completer = Completer<SaneHandle>();
127127

@@ -148,13 +148,13 @@ class Sane {
148148
}
149149

150150
Future<SaneHandle> openDevice(SaneDevice device) {
151-
if (_exited) throw StateError('SANE has been exited');
151+
_checkIfExited();
152152

153153
return open(device.name);
154154
}
155155

156156
Future<void> close(SaneHandle handle) {
157-
if (_exited) throw StateError('SANE has been exited');
157+
_checkIfExited();
158158

159159
final completer = Completer<void>();
160160

@@ -173,7 +173,7 @@ class Sane {
173173
SaneHandle handle,
174174
int index,
175175
) {
176-
if (_exited) throw StateError('SANE has been exited');
176+
_checkIfExited();
177177

178178
final completer = Completer<SaneOptionDescriptor>();
179179

@@ -196,7 +196,7 @@ class Sane {
196196
Future<List<SaneOptionDescriptor>> getAllOptionDescriptors(
197197
SaneHandle handle,
198198
) {
199-
if (_exited) throw StateError('SANE has been exited');
199+
_checkIfExited();
200200

201201
final completer = Completer<List<SaneOptionDescriptor>>();
202202

@@ -224,7 +224,7 @@ class Sane {
224224
required SaneAction action,
225225
T? value,
226226
}) {
227-
if (_exited) throw StateError('SANE has been exited');
227+
_checkIfExited();
228228

229229
final completer = Completer<SaneOptionResult<T>>();
230230

@@ -418,7 +418,7 @@ class Sane {
418418
}
419419

420420
Future<SaneParameters> getParameters(SaneHandle handle) {
421-
if (_exited) throw StateError('SANE has been exited');
421+
_checkIfExited();
422422

423423
final completer = Completer<SaneParameters>();
424424

@@ -443,7 +443,7 @@ class Sane {
443443
}
444444

445445
Future<void> start(SaneHandle handle) {
446-
if (_exited) throw StateError('SANE has been exited');
446+
_checkIfExited();
447447

448448
final completer = Completer<void>();
449449

@@ -460,7 +460,7 @@ class Sane {
460460
}
461461

462462
Future<Uint8List> read(SaneHandle handle, int bufferSize) {
463-
if (_exited) throw StateError('SANE has been exited');
463+
_checkIfExited();
464464

465465
final completer = Completer<Uint8List>();
466466

@@ -495,7 +495,7 @@ class Sane {
495495
}
496496

497497
Future<void> cancel(SaneHandle handle) {
498-
if (_exited) throw StateError('SANE has been exited');
498+
_checkIfExited();
499499

500500
final completer = Completer<void>();
501501

@@ -510,7 +510,7 @@ class Sane {
510510
}
511511

512512
Future<void> setIOMode(SaneHandle handle, SaneIOMode mode) {
513-
if (_exited) throw StateError('SANE has been exited');
513+
_checkIfExited();
514514

515515
final completer = Completer<void>();
516516

@@ -528,4 +528,9 @@ class Sane {
528528

529529
return completer.future;
530530
}
531+
532+
@pragma('vm:prefer-inline')
533+
void _checkIfExited() {
534+
if (_exited) throw SaneDisposedError();
535+
}
531536
}

0 commit comments

Comments
 (0)