forked from saschaludwig/OnAirScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
465 lines (356 loc) · 13.9 KB
/
exceptions.py
File metadata and controls
465 lines (356 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#############################################################################
#
# OnAirScreen
# Copyright (c) 2012-2026 Sascha Ludwig, astrastudio.de
# All rights reserved.
#
# exceptions.py
# This file is part of OnAirScreen
#
# You may use this file under the terms of the BSD license as follows:
#
# "Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
#
#############################################################################
"""
Custom Exceptions for OnAirScreen
This module defines a hierarchy of custom exceptions for consistent
error handling throughout the OnAirScreen application.
"""
import logging
from typing import Optional, Any
class OnAirScreenError(Exception):
"""
Base exception for all OnAirScreen errors
All custom exceptions in OnAirScreen should inherit from this class.
"""
def __init__(self, message: str, context: Optional[dict[str, Any]] = None) -> None:
"""
Initialize the exception
Args:
message: Error message
context: Optional context dictionary with additional information
"""
super().__init__(message)
self.message = message
self.context = context or {}
def __str__(self) -> str:
"""Return formatted error message with context"""
if self.context:
context_str = ", ".join(f"{k}={v}" for k, v in self.context.items())
return f"{self.message} ({context_str})"
return self.message
# Network Errors
class NetworkError(OnAirScreenError):
"""Base exception for all network-related errors"""
pass
class UdpError(NetworkError):
"""UDP-specific network errors"""
pass
class HttpError(NetworkError):
"""HTTP-specific network errors with status code"""
def __init__(self, message: str, status_code: int = 500, context: Optional[dict[str, Any]] = None) -> None:
"""
Initialize HTTP error
Args:
message: Error message
status_code: HTTP status code (default: 500)
context: Optional context dictionary
"""
super().__init__(message, context)
self.status_code = status_code
def __str__(self) -> str:
"""Return formatted error message with HTTP status code"""
base_msg = super().__str__()
return f"HTTP {self.status_code}: {base_msg}"
class WebSocketError(NetworkError):
"""WebSocket-specific network errors"""
pass
class MqttError(NetworkError):
"""MQTT-specific network errors"""
pass
class PortInUseError(NetworkError):
"""Port is already in use (errno 98)"""
def __init__(self, port: int, protocol: str = "TCP") -> None:
"""
Initialize port in use error
Args:
port: Port number that is in use
protocol: Protocol name (TCP, UDP, etc.)
"""
message = f"{protocol} port {port} is already in use"
super().__init__(message, {"port": port, "protocol": protocol})
self.port = port
self.protocol = protocol
class PermissionDeniedError(NetworkError):
"""Permission denied for port binding (errno 13)"""
def __init__(self, port: int, protocol: str = "TCP") -> None:
"""
Initialize permission denied error
Args:
port: Port number that requires permission
protocol: Protocol name (TCP, UDP, etc.)
"""
message = f"Permission denied binding to {protocol} port {port}"
super().__init__(message, {"port": port, "protocol": protocol})
self.port = port
self.protocol = protocol
# Command Errors
class CommandError(OnAirScreenError):
"""Base exception for all command-related errors"""
pass
class CommandParseError(CommandError):
"""Error parsing a command"""
def __init__(self, message: str, command_data: Optional[str] = None) -> None:
"""
Initialize command parse error
Args:
message: Error message
command_data: The command data that failed to parse
"""
context = {"command_data": command_data} if command_data else {}
super().__init__(message, context)
self.command_data = command_data
class CommandValidationError(CommandError):
"""Error validating a command value"""
def __init__(self, message: str, command: Optional[str] = None, value: Optional[str] = None) -> None:
"""
Initialize command validation error
Args:
message: Error message
command: Command name
value: Invalid value
"""
context = {}
if command:
context["command"] = command
if value:
context["value"] = value
super().__init__(message, context)
self.command = command
self.value = value
class UnknownCommandError(CommandError):
"""Unknown command received"""
def __init__(self, command: str) -> None:
"""
Initialize unknown command error
Args:
command: Unknown command name
"""
message = f"Unknown command: {command}"
super().__init__(message, {"command": command})
self.command = command
class InvalidCommandFormatError(CommandError):
"""Invalid command format"""
def __init__(self, message: str, command_data: Optional[str] = None) -> None:
"""
Initialize invalid command format error
Args:
message: Error message
command_data: The command data with invalid format
"""
context = {"command_data": command_data} if command_data else {}
super().__init__(message, context)
self.command_data = command_data
# Configuration Errors
class ConfigurationError(OnAirScreenError):
"""Base exception for all configuration-related errors"""
pass
class SettingsError(ConfigurationError):
"""Error loading or saving settings"""
pass
class InvalidConfigValueError(ConfigurationError):
"""Invalid configuration value"""
def __init__(self, message: str, key: Optional[str] = None, value: Optional[Any] = None) -> None:
"""
Initialize invalid config value error
Args:
message: Error message
key: Configuration key
value: Invalid value
"""
context = {}
if key:
context["key"] = key
if value is not None:
context["value"] = value
super().__init__(message, context)
self.key = key
self.value = value
# Validation Errors
class ValidationError(OnAirScreenError):
"""Base exception for all validation errors"""
pass
class TextValidationError(ValidationError):
"""Error validating text input"""
def __init__(self, message: str, field_name: Optional[str] = None, text: Optional[str] = None) -> None:
"""
Initialize text validation error
Args:
message: Error message
field_name: Name of the field being validated
text: Invalid text
"""
context = {}
if field_name:
context["field_name"] = field_name
if text:
context["text"] = text
super().__init__(message, context)
self.field_name = field_name
self.text = text
class ColorValidationError(ValidationError):
"""Error validating color value"""
def __init__(self, message: str, color_value: Optional[str] = None) -> None:
"""
Initialize color validation error
Args:
message: Error message
color_value: Invalid color value
"""
context = {"color_value": color_value} if color_value else {}
super().__init__(message, context)
self.color_value = color_value
class ValueValidationError(ValidationError):
"""Error validating a value"""
def __init__(self, message: str, value: Optional[Any] = None) -> None:
"""
Initialize value validation error
Args:
message: Error message
value: Invalid value
"""
context = {"value": value} if value is not None else {}
super().__init__(message, context)
self.value = value
# API Errors
class ApiError(OnAirScreenError):
"""Base exception for all API-related errors"""
pass
class WeatherApiError(ApiError):
"""OpenWeatherMap API error"""
def __init__(self, message: str, api_response: Optional[str] = None) -> None:
"""
Initialize weather API error
Args:
message: Error message
api_response: API response data (if available)
"""
context = {"api_response": api_response} if api_response else {}
super().__init__(message, context)
self.api_response = api_response
class JsonParseError(ApiError):
"""JSON parsing error"""
def __init__(self, message: str, json_data: Optional[str] = None) -> None:
"""
Initialize JSON parse error
Args:
message: Error message
json_data: The JSON data that failed to parse
"""
context = {"json_data": json_data} if json_data else {}
super().__init__(message, context)
self.json_data = json_data
class JsonSerializationError(ApiError):
"""JSON serialization error"""
def __init__(self, message: str, data: Optional[Any] = None) -> None:
"""
Initialize JSON serialization error
Args:
message: Error message
data: The data that failed to serialize
"""
context = {"data": str(data)} if data is not None else {}
super().__init__(message, context)
self.data = data
# Encoding Errors
class EncodingError(OnAirScreenError):
"""Encoding/decoding error (UTF-8, etc.)"""
def __init__(self, message: str, encoding: str = "utf-8", data: Optional[bytes] = None) -> None:
"""
Initialize encoding error
Args:
message: Error message
encoding: Encoding name (default: utf-8)
data: The data that failed to encode/decode
"""
context = {"encoding": encoding}
if data:
context["data_length"] = len(data)
super().__init__(message, context)
self.encoding = encoding
self.data = data
# Widget Errors
class WidgetError(OnAirScreenError):
"""Base exception for all widget-related errors"""
pass
class WidgetAccessError(WidgetError):
"""Error accessing widget properties"""
def __init__(self, message: str, widget_name: Optional[str] = None, attribute: Optional[str] = None) -> None:
"""
Initialize widget access error
Args:
message: Error message
widget_name: Name of the widget
attribute: Attribute that failed to access
"""
context = {}
if widget_name:
context["widget_name"] = widget_name
if attribute:
context["attribute"] = attribute
super().__init__(message, context)
self.widget_name = widget_name
self.attribute = attribute
# Logging Helper Function
def log_exception(logger: logging.Logger, exception: Exception, context: Optional[dict[str, Any]] = None,
use_exc_info: bool = True) -> None:
"""
Log an exception consistently
Args:
logger: Logger instance to use
exception: Exception to log
context: Optional additional context dictionary
use_exc_info: Whether to include exception traceback (default: True)
Set to False for expected validation errors
This function provides consistent logging for all exceptions in OnAirScreen.
For expected errors (like validation errors), set use_exc_info=False.
For unexpected errors, use_exc_info=True (default).
"""
# Build message with context
message = str(exception)
if context:
context_str = ", ".join(f"{k}={v}" for k, v in context.items())
message = f"{message} (context: {context_str})"
# Determine if we should use exc_info
# Don't use exc_info for expected validation/command errors
if isinstance(exception, (ValidationError, CommandValidationError, TextValidationError,
ColorValidationError, ValueValidationError, CommandParseError,
InvalidCommandFormatError, UnknownCommandError)):
use_exc_info = False
# Log the exception
if use_exc_info:
logger.error(message, exc_info=True)
else:
logger.error(message)