0
# Error Handling
1
2
Comprehensive exception hierarchy for handling all types of errors that can occur during GRIB/BUFR processing, from file access issues to data validation failures.
3
4
## Exception Hierarchy
5
6
### Base Exceptions
7
8
```python { .api }
9
class EcCodesError(Exception):
10
"""Base exception for all ecCodes errors."""
11
12
class GribInternalError(EcCodesError):
13
"""Internal error in GRIB processing."""
14
15
class BufferTooSmallError(EcCodesError):
16
"""Buffer size insufficient for operation."""
17
```
18
19
### File and I/O Errors
20
21
```python { .api }
22
class FileNotFoundError(EcCodesError):
23
"""File not found or cannot be opened."""
24
25
class IOProblemError(EcCodesError):
26
"""Input/output operation failed."""
27
28
class InvalidFileError(EcCodesError):
29
"""File format is invalid or corrupted."""
30
31
class PrematureEndOfFileError(EcCodesError):
32
"""Unexpected end of file encountered."""
33
```
34
35
### Data Access Errors
36
37
```python { .api }
38
class KeyValueNotFoundError(EcCodesError):
39
"""Requested key not found in message."""
40
41
class InvalidKeyValueError(EcCodesError):
42
"""Key value is invalid or out of range."""
43
44
class ReadOnlyError(EcCodesError):
45
"""Attempted to modify read-only key."""
46
47
class WrongArraySizeError(EcCodesError):
48
"""Array size mismatch in operation."""
49
```
50
51
### Message Processing Errors
52
53
```python { .api }
54
class InvalidMessageError(EcCodesError):
55
"""Message format is invalid."""
56
57
class DecodingError(EcCodesError):
58
"""Error decoding message data."""
59
60
class EncodingError(EcCodesError):
61
"""Error encoding message data."""
62
63
class UnsupportedEditionError(EcCodesError):
64
"""Unsupported GRIB/BUFR edition."""
65
```
66
67
### Memory and Resource Errors
68
69
```python { .api }
70
class OutOfMemoryError(EcCodesError):
71
"""Insufficient memory for operation."""
72
73
class NullHandleError(EcCodesError):
74
"""Invalid or null message handle."""
75
76
class InvalidArgumentError(EcCodesError):
77
"""Invalid argument passed to function."""
78
```
79
80
## Usage Examples
81
82
### Robust File Processing
83
84
```python
85
import eccodes
86
from eccodes import EcCodesError, FileNotFoundError, KeyValueNotFoundError
87
88
def safe_read_grib(filename):
89
try:
90
with open(filename, 'rb') as f:
91
msg = eccodes.codes_grib_new_from_file(f)
92
if msg is None:
93
print("No messages found in file")
94
return None
95
96
# Safe key access
97
try:
98
param_id = eccodes.codes_get(msg, 'paramId')
99
level = eccodes.codes_get(msg, 'level')
100
print(f"Parameter {param_id} at level {level}")
101
except KeyValueNotFoundError as e:
102
print(f"Key not found: {e}")
103
104
eccodes.codes_release(msg)
105
106
except FileNotFoundError:
107
print(f"File not found: {filename}")
108
except InvalidFileError:
109
print(f"Invalid file format: {filename}")
110
except EcCodesError as e:
111
print(f"ecCodes error: {e}")
112
113
# Usage
114
safe_read_grib('forecast.grib')
115
```