Skip to content

Commit 14acba5

Browse files
committed
Exceptions in separate file
1 parent 274d1b5 commit 14acba5

5 files changed

Lines changed: 101 additions & 91 deletions

File tree

camilladsp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from camilladsp.datastructures import (
77
ProcessingState,
88
StopReason,
9+
)
10+
from camilladsp.exceptions import (
911
CamillaError,
1012
InvalidRequestError,
1113
InvalidValueError,

camilladsp/camillaws.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import json
1010
from websocket import create_connection, WebSocket # type: ignore
1111

12-
from .datastructures import (
12+
from .exceptions import (
1313
CamillaError,
14-
raise_error,
14+
_raise_error,
1515
)
1616

1717

@@ -69,7 +69,7 @@ def _handle_reply(self, command: str, rawreply: Union[str, bytes]):
6969
value = response_data.get("value")
7070
if state == "Ok":
7171
return value
72-
raise_error(state, message, value)
72+
_raise_error(state, message, value)
7373
raise IOError(f"Invalid response received: {rawreply!r}")
7474
except json.JSONDecodeError as err:
7575
raise IOError(f"Invalid response received: {rawreply!r}") from err

camilladsp/datastructures.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from enum import Enum, auto
6-
from typing import Any, Optional, TypedDict
6+
from typing import Optional, TypedDict
77

88
_STANDARD_RATES = (
99
8000,
@@ -130,90 +130,6 @@ def _reason_from_reply(value):
130130
return reasonenum
131131

132132

133-
class CamillaError(Exception):
134-
"""
135-
Base class for errors returned by CamillaDSP.
136-
The 'message' attribute holds an optional message as a string,
137-
while the 'value' attribute holds an optional value.
138-
"""
139-
140-
def __init__(self, message: Optional[str] = None, value: Any = None):
141-
self.message = message
142-
self.value = value
143-
super().__init__(self.message)
144-
145-
146-
class ShutdownInProgressError(CamillaError):
147-
"""
148-
CamillaDSP is shutting down and unable to handle any more commands.
149-
"""
150-
151-
152-
class RateLimitExceededError(CamillaError):
153-
"""
154-
Too many requests were sent.
155-
"""
156-
157-
158-
class InvalidValueError(CamillaError):
159-
"""
160-
An invalid value was supplied with a command.
161-
"""
162-
163-
164-
class InvalidRequestError(CamillaError):
165-
"""
166-
An invalid command was sent.
167-
"""
168-
169-
170-
class InvalidFaderError(CamillaError):
171-
"""
172-
An invalid fader index was supplied with a command.
173-
"""
174-
175-
176-
class ConfigValidationError(CamillaError):
177-
"""
178-
The supplied config is invalid.
179-
"""
180-
181-
182-
class ConfigReadError(CamillaError):
183-
"""
184-
An error occurred while reading the config file.
185-
"""
186-
187-
188-
class UnknownError(CamillaError):
189-
"""
190-
An unknown error occurred.
191-
This should only happen if this library is used
192-
with a different version of CamillaDSP than it was intended for.
193-
"""
194-
195-
196-
def raise_error(state: str, message: Optional[str], value: Any):
197-
"""
198-
Raise the appropriate exception for the given error state.
199-
"""
200-
if state == "RateLimitExceededError":
201-
raise RateLimitExceededError()
202-
if state == "ShutdownInProgressError":
203-
raise ShutdownInProgressError()
204-
if state == "InvalidValueError":
205-
raise InvalidValueError(message=message, value=value)
206-
if state == "InvalidRequestError":
207-
raise InvalidRequestError(message=message, value=value)
208-
if state == "InvalidFaderError":
209-
raise InvalidFaderError(value=value)
210-
if state == "ConfigValidationError":
211-
raise ConfigValidationError(message=message, value=value)
212-
if state == "ConfigReadError":
213-
raise ConfigReadError(message=message, value=value)
214-
raise UnknownError(message=message, value=value)
215-
216-
217133
class Fader(TypedDict):
218134
"""
219135
Class for type annotation of fader volume and mute settings.

camilladsp/exceptions.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Exceptions that may be raised by this library.
3+
"""
4+
5+
from typing import Any, Optional
6+
7+
8+
class CamillaError(Exception):
9+
"""
10+
Base class for errors returned by CamillaDSP.
11+
The 'message' attribute holds an optional message as a string,
12+
while the 'value' attribute holds an optional value.
13+
"""
14+
15+
def __init__(self, message: Optional[str] = None, value: Any = None):
16+
self.message = message
17+
self.value = value
18+
super().__init__(self.message)
19+
20+
21+
class ShutdownInProgressError(CamillaError):
22+
"""
23+
CamillaDSP is shutting down and unable to handle any more commands.
24+
"""
25+
26+
27+
class RateLimitExceededError(CamillaError):
28+
"""
29+
Too many requests were sent.
30+
"""
31+
32+
33+
class InvalidValueError(CamillaError):
34+
"""
35+
An invalid value was supplied with a command.
36+
"""
37+
38+
39+
class InvalidRequestError(CamillaError):
40+
"""
41+
An invalid command was sent.
42+
"""
43+
44+
45+
class InvalidFaderError(CamillaError):
46+
"""
47+
An invalid fader index was supplied with a command.
48+
"""
49+
50+
51+
class ConfigValidationError(CamillaError):
52+
"""
53+
The supplied config is invalid.
54+
"""
55+
56+
57+
class ConfigReadError(CamillaError):
58+
"""
59+
An error occurred while reading the config file.
60+
"""
61+
62+
63+
class UnknownError(CamillaError):
64+
"""
65+
An unknown error occurred.
66+
This should only happen if this library is used
67+
with a different version of CamillaDSP than it was intended for.
68+
"""
69+
70+
71+
def _raise_error(state: str, message: Optional[str], value: Any):
72+
"""
73+
Raise the appropriate exception for the given error state.
74+
"""
75+
if state == "RateLimitExceededError":
76+
raise RateLimitExceededError()
77+
if state == "ShutdownInProgressError":
78+
raise ShutdownInProgressError()
79+
if state == "InvalidValueError":
80+
raise InvalidValueError(message=message, value=value)
81+
if state == "InvalidRequestError":
82+
raise InvalidRequestError(message=message, value=value)
83+
if state == "InvalidFaderError":
84+
raise InvalidFaderError(value=value)
85+
if state == "ConfigValidationError":
86+
raise ConfigValidationError(message=message, value=value)
87+
if state == "ConfigReadError":
88+
raise ConfigReadError(message=message, value=value)
89+
raise UnknownError(message=message, value=value)

docs/errors.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
# Errors
33

4-
The custom exception [CamillaError][camilladsp.datastructures.CamillaError] is raised when CamillaDSP replies to a command with an error message. The error message is given as the message of the exception.
4+
The exception [CamillaError][camilladsp.exceptions.CamillaError]
5+
or one of its subclasses is raised when CamillaDSP replies to a command with an error message.
6+
The error message is given as the message of the exception.
57

68
Different exceptions are raised in different situations. Consider the following example:
79
```python
@@ -24,5 +26,6 @@ except IOError as e:
2426
- `IOError` can mean a few things, but the most likely is that the websocket connection was lost.
2527
This happens if the CamillaDSP process exits or is restarted.
2628

27-
## CamillaError
28-
::: camilladsp.datastructures.CamillaError
29+
## Exceptions
30+
::: camilladsp.exceptions
31+

0 commit comments

Comments
 (0)