eccodes/
errors.rs

1//! Definition of errors returned by this crate
2//!
3//! This crate uses [`thiserror`] to define its error types.
4//!
5//! If you encounter an error that you believe is a result of implementation bug
6//! rather then user mistake post an issue on Github.
7
8use errno::Errno;
9use num_derive::FromPrimitive;
10use thiserror::Error;
11
12/// Errors returned by the all functions in the crate.
13#[derive(Error, Debug)]
14pub enum CodesError {
15    ///Returned when ecCodes library function returns an error code.
16    ///Check [`CodesInternal`] for more details.
17    #[error("ecCodes function returned a non-zero code {0}")]
18    Internal(#[from] CodesInternal),
19
20    ///Returned when one of libc functions returns a non-zero error code.
21    ///Check libc documentation for details of the errors.
22    ///For libc reference check these websites: ([1](https://man7.org/linux/man-pages/index.html))
23    ///([2](https://pubs.opengroup.org/onlinepubs/9699919799/functions/contents.html))
24    #[error("libc function returned an error with code {0} and errno {1}")]
25    LibcNonZero(i32, Errno),
26
27    ///Returned when there is an issue while handlng the file.
28    ///Check the [`std::fs`] documentation why and when this error can occur.
29    #[error("Error occured while opening the file: {0}")]
30    FileHandlingInterrupted(#[from] std::io::Error),
31
32    ///Returned when the string cannot be parsed as valid UTF8 string.
33    #[error("Cannot parse string as UTF8: {0}")]
34    CstrUTF8(#[from] std::str::Utf8Error),
35
36    ///Returned when the C-string returned by ecCodes library cannot be converted
37    ///into a Rust-string.
38    #[error("String returned by ecCodes is not nul terminated: {0}")]
39    NulChar(#[from] std::ffi::FromBytesWithNulError),
40
41    ///Returned when the requested key is not present in the message.
42    ///Similar to [`CodesInternal::CodesNotFound`] and [`CodesInternal::CodesMissingKey`].
43    #[error("The key is missing in present message")]
44    MissingKey,
45
46    /// Returned when the size of requested key is lower than 1.
47    /// This indicates corrupted data file, bug in the crate or bug in the ecCodes library.
48    #[error("Incorrect key size")]
49    IncorrectKeySize,
50
51    /// Returned when trying to read array as number.
52    #[error("Requested key size is incorrect")]
53    WrongRequestedKeySize,
54
55    /// Returned when trying to checked read key in non-native type.
56    #[error("Requested key type is incorrect")]
57    WrongRequestedKeyType,
58
59    /// Returned when [`eccodes_sys::codes_handle_clone`] returns null pointer
60    /// indicating issues with cloning the message.
61    #[error("Cannot clone the message")]
62    CloneFailed,
63
64    /// Returned when [`eccodes_sys::codes_keys_iterator_new`] returns null pointer
65    #[error("Cannot create or manipulate keys iterator")]
66    KeysIteratorFailed,
67
68    /// This error can be returned by almost any function in the crate.
69    /// It is returned when null pointer was passed to ecCodes function
70    /// that cannot handle null pointers. This error may indicate both
71    /// bug in the implementation or incorrect usage of the crate.
72    #[error("Null pointer encountered where it should not be")]
73    NullPtr,
74
75    /// Returned when function in `message_ndarray` module cannot convert
76    /// the message to ndarray. Check [`MessageNdarrayError`] for more details.
77    #[cfg(feature = "ndarray")]
78    #[error("error occured while converting CodesMessage to ndarray {0}")]
79    NdarrayConvert(#[from] MessageNdarrayError),
80
81    /// eccodes functions return errors as error codes and it is technically possible
82    /// that the library might return an error code that does not appear in [`CodesInternal`] enum.
83    #[error("eccodes returned unrecognized error code: {0}")]
84    UnrecognizedErrorCode(i32),
85
86    /// Similarly to error codes, eccodes return key type as i32, so
87    /// it's technically possible that this code does not appear in internal native key types.
88    #[error("Unrecognized native key type code: {0}")]
89    UnrecognizedKeyTypeCode(i32),
90}
91
92/// Errors returned by the `message_ndarray` module.
93#[cfg(feature = "ndarray")]
94#[cfg_attr(docsrs, doc(cfg(feature = "ndarray")))]
95#[derive(PartialEq, Clone, Error, Debug)]
96pub enum MessageNdarrayError {
97    /// Returned when functions converting to ndarray cannot correctly
98    /// read key necessary for the conversion.
99    #[error("Requested key {0} has a different type than expected")]
100    UnexpectedKeyType(String),
101
102    /// Returned when length of values array is not equal to
103    /// product of Ni and Nj keys.
104    #[error("The length of the values array ({0}) is different than expected ({1})")]
105    UnexpectedValuesLength(usize, usize),
106
107    /// Returned when functions converting to ndarray cannot correctly
108    /// parse key necessary for the conversion.
109    #[error("Requested key {0} has a value out of expected range")]
110    UnexpectedKeyValue(String),
111
112    /// Returned when ndarray cannot create an array with the shape
113    /// defined by Ni and Nj keys.
114    #[error("Error occured while converting to ndarray: {0}")]
115    InvalidShape(#[from] ndarray::ShapeError),
116
117    /// This error can occur when casting types of shape fails
118    /// on 32-bit systems or for very large arrays.
119    #[error(transparent)]
120    IntCasting(#[from] std::num::TryFromIntError),
121}
122
123///Errors returned by internal ecCodes library functions.
124///Copied directly from the ecCodes API.
125#[derive(Copy, Eq, PartialEq, Clone, Ord, PartialOrd, Hash, Error, Debug, FromPrimitive)]
126pub enum CodesInternal {
127    ///No error
128    #[error("No error")]
129    CodesSuccess = 0,
130
131    ///End of resource reached
132    #[error("End of resource reached")]
133    CodesEndOfFile = -1,
134
135    ///Internal error
136    #[error("Internal error")]
137    CodesInternalError = -2,
138
139    ///Passed buffer is too small
140    #[error("Passed buffer is too small")]
141    CodesBufferTooSmall = -3,
142
143    ///Function not yet implemented
144    #[error("Function not yet implemented")]
145    CodesNotImplemented = -4,
146
147    ///Missing 7777 at end of message
148    #[error("Missing 7777 at end of message")]
149    Codes7777NotFound = -5,
150
151    ///Passed array is too small
152    #[error("Passed array is too small")]
153    CodesArrayTooSmall = -6,
154
155    ///File not found
156    #[error("File not found")]
157    CodesFileNotFound = -7,
158
159    ///Code not found in code table
160    #[error("Code not found in code table")]
161    CodesCodeNotFoundInTable = -8,
162
163    ///Array size mismatch
164    #[error("Array size mismatch")]
165    CodesWrongArraySize = -9,
166
167    ///Key/value not found
168    #[error("Key/value not found")]
169    CodesNotFound = -10,
170
171    ///Input output problem
172    #[error("Input output problem")]
173    CodesIoProblem = -11,
174
175    ///Message invalid
176    #[error("Message invalid")]
177    CodesInvalidMessage = -12,
178
179    ///Decoding invalid
180    #[error("Decoding invalid")]
181    CodesDecodingError = -13,
182
183    ///Encoding invalid
184    #[error("Encoding invalid")]
185    CodesEncodingError = -14,
186
187    ///Code cannot unpack because of string too small
188    #[error("Code cannot unpack because of string too small")]
189    CodesNoMoreInSet = -15,
190
191    ///Problem with calculation of geographic attributes
192    #[error("Problem with calculation of geographic attributes")]
193    CodesGeocalculusProblem = -16,
194
195    ///Memory allocation error
196    #[error("Memory allocation error")]
197    CodesOutOfMemory = -17,
198
199    ///Value is read only
200    #[error("Value is read only")]
201    CodesReadOnly = -18,
202
203    ///Invalid argument
204    #[error("Invalid argument")]
205    CodesInvalidArgument = -19,
206
207    ///Null handle
208    #[error("Null handle")]
209    CodesNullHandle = -20,
210
211    ///Invalid section number
212    #[error("Invalid section number")]
213    CodesInvalidSectionNumber = -21,
214
215    ///Value cannot be missing
216    #[error("Value cannot be missing")]
217    CodesValueCannotBeMissing = -22,
218
219    ///Wrong message length
220    #[error("Wrong message length")]
221    CodesWrongLength = -23,
222
223    ///Invalid key type
224    #[error("Invalid key type")]
225    CodesInvalidType = -24,
226
227    ///Unable to set step
228    #[error("Unable to set step")]
229    CodesWrongStep = -25,
230
231    ///Wrong units for step (step must be integer)
232    #[error("Wrong units for step (step must be integer)")]
233    CodesWrongStepUnit = -26,
234
235    ///Invalid file id
236    #[error("Invalid file id")]
237    CodesInvalidFile = -27,
238
239    ///Invalid grib id
240    #[error("Invalid grib id")]
241    CodesInvalidGrib = -28,
242
243    ///Invalid index id
244    #[error("Invalid index id")]
245    CodesInvalidIndex = -29,
246
247    ///Invalid iterator id
248    #[error("Invalid iterator id")]
249    CodesInvalidIterator = -30,
250
251    ///Invalid keys iterator id
252    #[error("Invalid keys iterator id")]
253    CodesInvalidKeysIterator = -31,
254
255    ///Invalid nearest id
256    #[error("Invalid nearest id")]
257    CodesInvalidNearest = -32,
258
259    ///Invalid order by
260    #[error("Invalid order by")]
261    CodesInvalidOrderby = -33,
262
263    ///Missing a key from the fieldset
264    #[error("Missing a key from the fieldset")]
265    CodesMissingKey = -34,
266
267    ///The point is out of the grid area
268    #[error("The point is out of the grid area")]
269    CodesOutOfArea = -35,
270
271    ///Concept no match
272    #[error("Concept no match")]
273    CodesConceptNoMatch = -36,
274
275    ///Hash array no match
276    #[error("Hash array no match")]
277    CodesHashArrayNoMatch = -37,
278
279    ///Definitions files not found
280    #[error("Definitions files not found")]
281    CodesNoDefinitions = -38,
282
283    ///Wrong type while packing
284    #[error("Wrong type while packing")]
285    CodesWrongType = -39,
286
287    ///End of resource
288    #[error("End of resource")]
289    CodesEnd = -40,
290
291    ///Unable to code a field without values
292    #[error("Unable to code a field without values")]
293    CodesNoValues = -41,
294
295    ///Grid description is wrong or inconsistent
296    #[error("Grid description is wrong or inconsistent")]
297    CodesWrongGrid = -42,
298
299    ///End of index reached
300    #[error("End of index reached")]
301    CodesEndOfIndex = -43,
302
303    ///Null index
304    #[error("Null index")]
305    CodesNullIndex = -44,
306
307    ///End of resource reached when reading message
308    #[error("End of resource reached when reading message")]
309    CodesPrematureEndOfFile = -45,
310
311    ///An internal array is too small
312    #[error("An internal array is too small")]
313    CodesInternalArrayTooSmall = -46,
314
315    ///Message is too large for the current architecture
316    #[error("Message is too large for the current architecture")]
317    CodesMessageTooLarge = -47,
318
319    ///Constant field
320    #[error("Constant field")]
321    CodesConstantField = -48,
322
323    ///Switch unable to find a matching case
324    #[error("Switch unable to find a matching case")]
325    CodesSwitchNoMatch = -49,
326
327    ///Underflow
328    #[error("Underflow")]
329    CodesUnderflow = -50,
330
331    ///Message malformed
332    #[error("Message malformed")]
333    CodesMessageMalformed = -51,
334
335    ///Index is corrupted
336    #[error("Index is corrupted")]
337    CodesCorruptedIndex = -52,
338
339    ///Invalid number of bits per value
340    #[error("Invalid number of bits per value")]
341    CodesInvalidBpv = -53,
342
343    ///Edition of two messages is different
344    #[error("Edition of two messages is different")]
345    CodesDifferentEdition = -54,
346
347    ///Value is different
348    #[error("Value is different")]
349    CodesValueDifferent = -55,
350
351    ///Invalid key value
352    #[error("Invalid key value")]
353    CodesInvalidKeyValue = -56,
354
355    ///String is smaller than requested
356    #[error("String is smaller than requested")]
357    CodesStringTooSmall = -57,
358
359    ///Wrong type conversion
360    #[error("Wrong type conversion")]
361    CodesWrongConversion = -58,
362
363    ///Missing BUFR table entry for descriptor
364    #[error("Missing BUFR table entry for descriptor")]
365    CodesMissingBufrEntry = -59,
366
367    ///Null pointer
368    #[error("Null pointer")]
369    CodesNullPointer = -60,
370
371    ///Attribute is already present =  cannot add
372    #[error("Attribute is already present =  cannot add")]
373    CodesAttributeClash = -61,
374
375    ///Too many attributes. Increase `MAX_ACCESSOR_ATTRIBUTES`
376    #[error("Too many attributes. Increase MAX_ACCESSOR_ATTRIBUTES")]
377    CodesTooManyAttributes = -62,
378
379    ///Attribute not found
380    #[error("Attribute not found")]
381    CodesAttributeNotFound = -63,
382
383    ///Edition not supported
384    #[error("Edition not supported")]
385    CodesUnsupportedEdition = -64,
386
387    ///Value out of coding range
388    #[error("Value out of coding range")]
389    CodesOutOfRange = -65,
390
391    ///Size of bitmap is incorrect
392    #[error("Size of bitmap is incorrect")]
393    CodesWrongBitmapSize = -66,
394
395    ///Functionality not enabled
396    #[error("Functionality not enabled")]
397    CodesFunctionalityNotEnabled = -67,
398}