Skip to content

Commit 0a313ac

Browse files
♻️ refactor: Refactored error handling for improved readability and consistency.
1 parent 4f51912 commit 0a313ac

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

squarecloud/cli/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from squarecloud.client import Client, create_config_file
1212
from squarecloud.data import StatisticsData
1313

14-
from .. import ApplicationNotFound, RequestError, SquareException
14+
from ..errors import RequestError, SquareException
1515
from . import cli, run_async
1616
from .app import app_list, upload_app
1717
from .files import app_group

squarecloud/errors.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from .file import File
2+
3+
14
class SquareException(BaseException):
25
"""abstract class SquareException"""
36

@@ -11,7 +14,9 @@ def __str__(self):
1114
class RequestError(SquareException):
1215
"""raised when a request fails"""
1316

14-
def __init__(self, route: str, status_code: int, code: str):
17+
def __init__(
18+
self, route: str, status_code: int, code: str, *args, **kwargs
19+
):
1520
self.route = route
1621
self.status = status_code
1722
self.code = code
@@ -102,3 +107,93 @@ class BadMemory(RequestError):
102107
def __init__(self, *args, **kwargs):
103108
super().__init__(*args, **kwargs)
104109
self.message = 'No memory available'
110+
111+
112+
class InvalidConfig(RequestError):
113+
"""
114+
raised when the config file is corrupt or invalid.
115+
"""
116+
117+
def __init__(self, file: File, *args, **kwargs):
118+
super().__init__(*args, **kwargs)
119+
self.message = f'{file.filename} have a invalid config file'
120+
121+
122+
class InvalidDisplayName(InvalidConfig):
123+
"""
124+
raised when the display name in the config file is invalid.
125+
"""
126+
127+
def __init__(self, file: File, *args, **kwargs):
128+
super().__init__(file=file, *args, **kwargs)
129+
self.message = 'Invalid display name in config file'
130+
131+
132+
class MissingDisplayName(InvalidConfig):
133+
"""
134+
raised when the display name in the config file is missing.
135+
"""
136+
137+
def __init__(self, *args, **kwargs):
138+
super().__init__(*args, **kwargs)
139+
self.message = 'Display name is missing in the config file'
140+
141+
142+
class InvalidMain(InvalidConfig):
143+
"""
144+
raised when the main file in the config file is invalid.
145+
"""
146+
147+
def __init__(self, file: File, *args, **kwargs):
148+
super().__init__(file=file, *args, **kwargs)
149+
self.message = 'Invalid main file in config file'
150+
151+
152+
class MissingMainFile(InvalidConfig):
153+
"""
154+
raised when the main file in the config file is missing.
155+
"""
156+
157+
def __init__(self, *args, **kwargs):
158+
super().__init__(*args, **kwargs)
159+
self.message = 'Main file is missing in the config file'
160+
161+
162+
class InvalidMemory(InvalidConfig):
163+
"""
164+
raised when the memory value in the config file is invalid.
165+
"""
166+
167+
def __init__(self, file: File, *args, **kwargs):
168+
super().__init__(file=file, *args, **kwargs)
169+
self.message = 'Invalid memory value in config file'
170+
171+
172+
class MissingMemory(InvalidConfig):
173+
"""
174+
raised when the memory value in the config file is missing.
175+
"""
176+
177+
def __init__(self, *args, **kwargs):
178+
super().__init__(*args, **kwargs)
179+
self.message = 'Memory value is missing in the config file'
180+
181+
182+
class InvalidVersion(InvalidConfig):
183+
"""
184+
raised when the version value in the config file is invalid.
185+
"""
186+
187+
def __init__(self, file: File, *args, **kwargs):
188+
super().__init__(file=file, *args, **kwargs)
189+
self.message = 'Invalid version value in config file'
190+
191+
192+
class MissingVersion(InvalidConfig):
193+
"""
194+
raised when the version value in the config file is missing.
195+
"""
196+
197+
def __init__(self, *args, **kwargs):
198+
super().__init__(*args, **kwargs)
199+
self.message = 'Version value is missing in the config file'

squarecloud/http/http_client.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
BadMemory,
1212
BadRequestError,
1313
FewMemory,
14+
InvalidDisplayName,
15+
InvalidMain,
16+
InvalidMemory,
17+
InvalidVersion,
1418
MissingConfigFile,
1519
MissingDependenciesFile,
20+
MissingDisplayName,
21+
MissingMainFile,
22+
MissingMemory,
23+
MissingVersion,
1624
NotFoundError,
1725
RequestError,
1826
TooManyRequests,
@@ -57,12 +65,20 @@ def __repr__(self):
5765
return f'{Response.__name__}({self.data})'
5866

5967

60-
def get_upload_error(code: str) -> Type[RequestError]:
68+
def _get_upload_error(code: str) -> type[RequestError]:
6169
errors = {
6270
'FEW_MEMORY': FewMemory,
6371
'BAD_MEMORY': BadMemory,
64-
'MISSING_CONFIG_FILE': MissingConfigFile,
72+
'MISSING_CONFIG': MissingConfigFile,
6573
'MISSING_DEPENDENCIES_FILE': MissingDependenciesFile,
74+
'MISSING_MAIN': MissingMainFile,
75+
'INVALID_MAIN': InvalidMain,
76+
'INVALID_DISPLAY_NAME': InvalidDisplayName,
77+
'MISSING_DISPLAY_NAME': MissingDisplayName,
78+
'INVALID_MEMORY': InvalidMemory,
79+
'MISSING_MEMORY': MissingMemory,
80+
'INVALID_VERSION': InvalidVersion,
81+
'MISSING_VERSION': MissingVersion,
6682
}
6783
error_class = errors.get(code, None)
6884
if error_class is None:
@@ -98,9 +114,11 @@ async def request(self, route: Router, **kwargs) -> Response | bytes:
98114
RawResponseData
99115
"""
100116
headers = {'Authorization': self.api_key}
117+
extra_error_kwargs: dict[str, Any] = {}
101118

102119
if route.endpoint in (Endpoint.commit(), Endpoint.upload()):
103120
file = kwargs.pop('file')
121+
extra_error_kwargs['file'] = file
104122
form = aiohttp.FormData()
105123
form.add_field('file', file.bytes, filename=file.filename)
106124
kwargs['data'] = form
@@ -118,10 +136,16 @@ async def request(self, route: Router, **kwargs) -> Response | bytes:
118136
}
119137
code: str | None = data.get('code')
120138
error: Type[RequestError] | None = None
139+
121140
if status_code == 200:
122141
logger.debug(msg='request to route: ', extra=extra)
123-
if code and route.endpoint == Endpoint.upload():
124-
error = get_upload_error(code)
142+
if code and route.endpoint in (
143+
Endpoint.commit(),
144+
Endpoint.upload(),
145+
):
146+
error = _get_upload_error(
147+
code,
148+
)
125149
extra.pop('code')
126150
response: Response = Response(data=data, route=route)
127151
elif status_code == 404:
@@ -144,6 +168,7 @@ async def request(self, route: Router, **kwargs) -> Response | bytes:
144168
error = RequestError
145169
if error:
146170
raise error(
171+
**extra_error_kwargs,
147172
route=route.endpoint.name,
148173
status_code=status_code,
149174
code=code,

0 commit comments

Comments
 (0)