From 66f9f2ada48523962e03f9d0f25727d62399d6a6 Mon Sep 17 00:00:00 2001 From: Craftplacer <22963120+craftplacer@users.noreply.github.com> Date: Mon, 4 Nov 2024 10:08:56 +0100 Subject: [PATCH] Add dedicated error type and helper method --- packages/sane/lib/src/exceptions.dart | 5 +++++ packages/sane/lib/src/sane.dart | 29 ++++++++++++++++----------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/sane/lib/src/exceptions.dart b/packages/sane/lib/src/exceptions.dart index 2863818..1ff0ae1 100644 --- a/packages/sane/lib/src/exceptions.dart +++ b/packages/sane/lib/src/exceptions.dart @@ -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'); +} diff --git a/packages/sane/lib/src/sane.dart b/packages/sane/lib/src/sane.dart index 420dd94..e1ccbde 100644 --- a/packages/sane/lib/src/sane.dart +++ b/packages/sane/lib/src/sane.dart @@ -91,7 +91,7 @@ class Sane { Future> getDevices({ required bool localOnly, }) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer>(); @@ -121,7 +121,7 @@ class Sane { } Future open(String deviceName) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -148,13 +148,13 @@ class Sane { } Future openDevice(SaneDevice device) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); return open(device.name); } Future close(SaneHandle handle) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -173,7 +173,7 @@ class Sane { SaneHandle handle, int index, ) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -196,7 +196,7 @@ class Sane { Future> getAllOptionDescriptors( SaneHandle handle, ) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer>(); @@ -224,7 +224,7 @@ class Sane { required SaneAction action, T? value, }) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer>(); @@ -418,7 +418,7 @@ class Sane { } Future getParameters(SaneHandle handle) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -443,7 +443,7 @@ class Sane { } Future start(SaneHandle handle) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -460,7 +460,7 @@ class Sane { } Future read(SaneHandle handle, int bufferSize) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -495,7 +495,7 @@ class Sane { } Future cancel(SaneHandle handle) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -510,7 +510,7 @@ class Sane { } Future setIOMode(SaneHandle handle, SaneIOMode mode) { - if (_exited) throw StateError('SANE has been exited'); + _checkIfExited(); final completer = Completer(); @@ -528,4 +528,9 @@ class Sane { return completer.future; } + + @pragma('vm:prefer-inline') + void _checkIfExited() { + if (_exited) throw SaneDisposedError(); + } }