Skip to content

Commit

Permalink
Add dedicated error type and helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftplacer committed Nov 4, 2024
1 parent 5f05f3c commit 66f9f2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/sane/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ final class SaneUnsupportedException extends SaneException {
@override
SANE_Status get _status => SANE_Status.STATUS_UNSUPPORTED;
}

/// SANE has been exited or the device has been closed.
final class SaneDisposedError extends StateError {
SaneDisposedError() : super('SANE has been exited');
}
29 changes: 17 additions & 12 deletions packages/sane/lib/src/sane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Sane {
Future<List<SaneDevice>> getDevices({
required bool localOnly,
}) {
if (_exited) throw StateError('SANE has been exited');
_checkIfExited();

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

Expand Down Expand Up @@ -121,7 +121,7 @@ class Sane {
}

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

final completer = Completer<SaneHandle>();

Expand All @@ -148,13 +148,13 @@ class Sane {
}

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

return open(device.name);
}

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

final completer = Completer<void>();

Expand All @@ -173,7 +173,7 @@ class Sane {
SaneHandle handle,
int index,
) {
if (_exited) throw StateError('SANE has been exited');
_checkIfExited();

final completer = Completer<SaneOptionDescriptor>();

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

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

Expand Down Expand Up @@ -224,7 +224,7 @@ class Sane {
required SaneAction action,
T? value,
}) {
if (_exited) throw StateError('SANE has been exited');
_checkIfExited();

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

Expand Down Expand Up @@ -418,7 +418,7 @@ class Sane {
}

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

final completer = Completer<SaneParameters>();

Expand All @@ -443,7 +443,7 @@ class Sane {
}

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

final completer = Completer<void>();

Expand All @@ -460,7 +460,7 @@ class Sane {
}

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

final completer = Completer<Uint8List>();

Expand Down Expand Up @@ -495,7 +495,7 @@ class Sane {
}

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

final completer = Completer<void>();

Expand All @@ -510,7 +510,7 @@ class Sane {
}

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

final completer = Completer<void>();

Expand All @@ -528,4 +528,9 @@ class Sane {

return completer.future;
}

@pragma('vm:prefer-inline')
void _checkIfExited() {
if (_exited) throw SaneDisposedError();
}
}

0 comments on commit 66f9f2a

Please sign in to comment.