1- abstract class SaneException implements Exception {}
1+ import 'package:ffi/ffi.dart' ;
2+ import 'package:meta/meta.dart' ;
3+ import 'package:sane/src/bindings.g.dart' ;
4+ import 'package:sane/src/dylib.dart' ;
25
3- class SaneUnsupportedException extends SaneException {}
6+ /// Base class for all possible errors that can occur in the SANE library.
7+ ///
8+ /// See also:
9+ ///
10+ /// - [SaneEofException]
11+ /// - [SaneJammedException]
12+ /// - [SaneDeviceBusyException]
13+ /// - [SaneInvalidDataException]
14+ /// - [SaneIoException]
15+ /// - [SaneNoDocumentsException]
16+ /// - [SaneCoverOpenException]
17+ /// - [SaneUnsupportedException]
18+ /// - [SaneCancelledException]
19+ /// - [SaneNoMemoryException]
20+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
21+ sealed class SaneException implements Exception {
22+ SANE_Status get _status;
423
5- class SaneCancelledException extends SaneException {}
24+ const SaneException ._();
625
7- class SaneDeviceBusyException extends SaneException {}
26+ factory SaneException (SANE_Status status) {
27+ final exception = switch (status) {
28+ SANE_Status .STATUS_GOOD =>
29+ throw ArgumentError (
30+ 'Cannot create SaneException with status STATUS_GOOD' ,
31+ 'status' ,
32+ ),
33+ SANE_Status .STATUS_UNSUPPORTED => SaneUnsupportedException (),
34+ SANE_Status .STATUS_CANCELLED => SaneCancelledException (),
35+ SANE_Status .STATUS_DEVICE_BUSY => SaneDeviceBusyException (),
36+ SANE_Status .STATUS_INVAL => SaneInvalidDataException (),
37+ SANE_Status .STATUS_EOF => SaneEofException (),
38+ SANE_Status .STATUS_JAMMED => SaneJammedException (),
39+ SANE_Status .STATUS_NO_DOCS => SaneNoDocumentsException (),
40+ SANE_Status .STATUS_COVER_OPEN => SaneCoverOpenException (),
41+ SANE_Status .STATUS_IO_ERROR => SaneIoException (),
42+ SANE_Status .STATUS_NO_MEM => SaneNoMemoryException (),
43+ SANE_Status .STATUS_ACCESS_DENIED => SaneAccessDeniedException (),
44+ };
845
9- class SaneInvalidException extends SaneException {}
46+ assert (exception._status == status);
1047
11- class SaneJammedException extends SaneException {}
48+ return exception;
49+ }
1250
13- class SaneNoDocumentsException extends SaneException {}
51+ String get message {
52+ return dylib.sane_strstatus (_status).cast <Utf8 >().toDartString ();
53+ }
1454
15- class SaneCoverOpenException extends SaneException {}
55+ @override
56+ String toString () {
57+ return '$runtimeType : $message ' ;
58+ }
59+ }
1660
17- class SaneIOErrorException extends SaneException {}
61+ /// No more data available.
62+ ///
63+ /// See also:
64+ ///
65+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
66+ final class SaneEofException extends SaneException {
67+ const SaneEofException () : super ._();
1868
19- class SaneNoMemoryException extends SaneException {}
69+ @override
70+ SANE_Status get _status => SANE_Status .STATUS_EOF ;
71+ }
2072
21- class SaneAccessDeniedException extends SaneException {}
73+ /// The document feeder is jammed.
74+ ///
75+ /// See also:
76+ ///
77+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
78+ final class SaneJammedException extends SaneException {
79+ const SaneJammedException () : super ._();
2280
23- class SaneNotFoundOption extends SaneException {}
81+ @override
82+ SANE_Status get _status => SANE_Status .STATUS_JAMMED ;
83+ }
84+
85+ /// The document feeder is out of documents.
86+ ///
87+ /// See also:
88+ ///
89+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
90+ final class SaneNoDocumentsException extends SaneException {
91+ const SaneNoDocumentsException () : super ._();
92+
93+ @override
94+ SANE_Status get _status => SANE_Status .STATUS_NO_DOCS ;
95+ }
96+
97+ /// The scanner cover is open.
98+ ///
99+ /// See also:
100+ ///
101+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
102+ final class SaneCoverOpenException extends SaneException {
103+ const SaneCoverOpenException () : super ._();
104+
105+ @override
106+ SANE_Status get _status => SANE_Status .STATUS_COVER_OPEN ;
107+ }
108+
109+ /// The device is busy.
110+ ///
111+ /// See also:
112+ ///
113+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
114+ final class SaneDeviceBusyException extends SaneException {
115+ const SaneDeviceBusyException () : super ._();
116+
117+ @override
118+ SANE_Status get _status => SANE_Status .STATUS_DEVICE_BUSY ;
119+ }
120+
121+ /// Data is invalid.
122+ ///
123+ /// See also:
124+ ///
125+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
126+ final class SaneInvalidDataException extends SaneException {
127+ const SaneInvalidDataException () : super ._();
128+
129+ @override
130+ SANE_Status get _status => SANE_Status .STATUS_INVAL ;
131+ }
132+
133+ /// Error during device I/O.
134+ ///
135+ /// See also:
136+ ///
137+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
138+ final class SaneIoException extends SaneException {
139+ const SaneIoException () : super ._();
140+
141+ @override
142+ SANE_Status get _status => SANE_Status .STATUS_IO_ERROR ;
143+ }
144+
145+ /// Out of memory.
146+ ///
147+ /// See also:
148+ ///
149+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
150+ final class SaneNoMemoryException extends SaneException {
151+ const SaneNoMemoryException () : super ._();
152+
153+ @override
154+ SANE_Status get _status => SANE_Status .STATUS_NO_MEM ;
155+ }
156+
157+ /// Access to resource has been denied.
158+ ///
159+ /// See also:
160+ ///
161+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
162+ final class SaneAccessDeniedException extends SaneException {
163+ const SaneAccessDeniedException () : super ._();
164+
165+ @override
166+ SANE_Status get _status => SANE_Status .STATUS_ACCESS_DENIED ;
167+ }
168+
169+ /// Operation was cancelled.
170+ ///
171+ /// See also:
172+ ///
173+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
174+ final class SaneCancelledException extends SaneException {
175+ const SaneCancelledException () : super ._();
176+
177+ @override
178+ SANE_Status get _status => SANE_Status .STATUS_CANCELLED ;
179+ }
180+
181+ /// Operation is not supported.
182+ ///
183+ /// See also:
184+ ///
185+ /// - <https://sane-project.gitlab.io/standard/api.html#tab-status>
186+ final class SaneUnsupportedException extends SaneException {
187+ const SaneUnsupportedException () : super ._();
188+
189+ @override
190+ SANE_Status get _status => SANE_Status .STATUS_UNSUPPORTED ;
191+ }
192+
193+ @internal
194+ extension SaneStatusExtension on SANE_Status {
195+ /// Throws [SaneException] if the status is not [SANE_Status.STATUS_GOOD] .
196+ @pragma ('vm:prefer-inline' )
197+ void check () {
198+ if (this != SANE_Status .STATUS_GOOD ) {
199+ throw SaneException (this );
200+ }
201+ }
202+ }
0 commit comments