Skip to content

Commit 761598d

Browse files
author
Albert Suralinski
committed
PLINT-1175 Allow specifying error message
1 parent 948bfcd commit 761598d

5 files changed

Lines changed: 180 additions & 51 deletions

File tree

src/galaxy/api/errors.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,87 @@
22

33
assert UnknownError
44

5+
56
class AuthenticationRequired(ApplicationError):
6-
def __init__(self, data=None):
7-
super().__init__(1, "Authentication required", data)
7+
def __init__(self, message="Authentication required", data=None):
8+
super().__init__(1, message, data)
9+
810

911
class BackendNotAvailable(ApplicationError):
10-
def __init__(self, data=None):
11-
super().__init__(2, "Backend not available", data)
12+
def __init__(self, message="Backend not available", data=None):
13+
super().__init__(2, message, data)
14+
1215

1316
class BackendTimeout(ApplicationError):
14-
def __init__(self, data=None):
15-
super().__init__(3, "Backend timed out", data)
17+
def __init__(self, message="Backend timed out", data=None):
18+
super().__init__(3, message, data)
19+
1620

1721
class BackendError(ApplicationError):
18-
def __init__(self, data=None):
19-
super().__init__(4, "Backend error", data)
22+
def __init__(self, message="Backend error", data=None):
23+
super().__init__(4, message, data)
24+
2025

2126
class UnknownBackendResponse(ApplicationError):
22-
def __init__(self, data=None):
23-
super().__init__(4, "Backend responded in unknown way", data)
27+
def __init__(self, message="Backend responded in unknown way", data=None):
28+
super().__init__(4, message, data)
29+
2430

2531
class TooManyRequests(ApplicationError):
26-
def __init__(self, data=None):
27-
super().__init__(5, "Too many requests. Try again later", data)
32+
def __init__(self, message="Too many requests. Try again later", data=None):
33+
super().__init__(5, message, data)
34+
2835

2936
class InvalidCredentials(ApplicationError):
30-
def __init__(self, data=None):
31-
super().__init__(100, "Invalid credentials", data)
37+
def __init__(self, message="Invalid credentials", data=None):
38+
super().__init__(100, message, data)
39+
3240

3341
class NetworkError(ApplicationError):
34-
def __init__(self, data=None):
35-
super().__init__(101, "Network error", data)
42+
def __init__(self, message="Network error", data=None):
43+
super().__init__(101, message, data)
44+
3645

3746
class ProtocolError(ApplicationError):
38-
def __init__(self, data=None):
39-
super().__init__(103, "Protocol error", data)
47+
def __init__(self, message="Protocol error", data=None):
48+
super().__init__(103, message, data)
49+
4050

4151
class TemporaryBlocked(ApplicationError):
42-
def __init__(self, data=None):
43-
super().__init__(104, "Temporary blocked", data)
52+
def __init__(self, message="Temporary blocked", data=None):
53+
super().__init__(104, message, data)
54+
4455

4556
class Banned(ApplicationError):
46-
def __init__(self, data=None):
47-
super().__init__(105, "Banned", data)
57+
def __init__(self, message="Banned", data=None):
58+
super().__init__(105, message, data)
59+
4860

4961
class AccessDenied(ApplicationError):
50-
def __init__(self, data=None):
51-
super().__init__(106, "Access denied", data)
62+
def __init__(self, message="Access denied", data=None):
63+
super().__init__(106, message, data)
64+
5265

5366
class FailedParsingManifest(ApplicationError):
54-
def __init__(self, data=None):
55-
super().__init__(200, "Failed parsing manifest", data)
67+
def __init__(self, message="Failed parsing manifest", data=None):
68+
super().__init__(200, message, data)
69+
5670

5771
class TooManyMessagesSent(ApplicationError):
58-
def __init__(self, data=None):
59-
super().__init__(300, "Too many messages sent", data)
72+
def __init__(self, message="Too many messages sent", data=None):
73+
super().__init__(300, message, data)
74+
6075

6176
class IncoherentLastMessage(ApplicationError):
62-
def __init__(self, data=None):
63-
super().__init__(400, "Different last message id on backend", data)
77+
def __init__(self, message="Different last message id on backend", data=None):
78+
super().__init__(400, message, data)
79+
6480

6581
class MessageNotFound(ApplicationError):
66-
def __init__(self, data=None):
67-
super().__init__(500, "Message not found", data)
82+
def __init__(self, message="Message not found", data=None):
83+
super().__init__(500, message, data)
84+
6885

6986
class ImportInProgress(ApplicationError):
70-
def __init__(self, data=None):
71-
super().__init__(600, "Import already in progress", data)
87+
def __init__(self, message="Import already in progress", data=None):
88+
super().__init__(600, message, data)

src/galaxy/api/jsonrpc.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class JsonRpcError(Exception):
1616
def __init__(self, code, message, data=None):
1717
self.code = code
18-
self.message = message
18+
self.message = str(message)
1919
self.data = data
2020
super().__init__()
2121

@@ -33,39 +33,48 @@ def json(self):
3333

3434
return obj
3535

36+
3637
class ParseError(JsonRpcError):
37-
def __init__(self):
38-
super().__init__(-32700, "Parse error")
38+
def __init__(self, message="Parse error"):
39+
super().__init__(-32700, message)
40+
3941

4042
class InvalidRequest(JsonRpcError):
41-
def __init__(self):
42-
super().__init__(-32600, "Invalid Request")
43+
def __init__(self, message="Invalid Request"):
44+
super().__init__(-32600, message)
45+
4346

4447
class MethodNotFound(JsonRpcError):
45-
def __init__(self):
46-
super().__init__(-32601, "Method not found")
48+
def __init__(self, message="Method not found"):
49+
super().__init__(-32601, message)
50+
4751

4852
class InvalidParams(JsonRpcError):
49-
def __init__(self):
50-
super().__init__(-32602, "Invalid params")
53+
def __init__(self, message="Invalid params"):
54+
super().__init__(-32602, message)
55+
5156

5257
class Timeout(JsonRpcError):
53-
def __init__(self):
54-
super().__init__(-32000, "Method timed out")
58+
def __init__(self, message="Method timed out"):
59+
super().__init__(-32000, message)
60+
5561

5662
class Aborted(JsonRpcError):
57-
def __init__(self):
58-
super().__init__(-32001, "Method aborted")
63+
def __init__(self, message="Method aborted"):
64+
super().__init__(-32001, message)
65+
5966

6067
class ApplicationError(JsonRpcError):
6168
def __init__(self, code, message, data):
6269
if code >= -32768 and code <= -32000:
6370
raise ValueError("The error code in reserved range")
6471
super().__init__(code, message, data)
6572

73+
6674
class UnknownError(ApplicationError):
67-
def __init__(self, data=None):
68-
super().__init__(0, "Unknown error", data)
75+
def __init__(self, message="Unknown error", data=None):
76+
super().__init__(0, message, data)
77+
6978

7079
Request = namedtuple("Request", ["method", "params", "id"], defaults=[{}, None])
7180
Response = namedtuple("Response", ["id", "result", "error"], defaults=[None, {}, {}])

tests/test_errors.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import pytest
2+
import galaxy.api.errors as errors
3+
import galaxy.api.jsonrpc as jsonrpc
4+
5+
6+
@pytest.mark.parametrize("error, expected_error_msg", [
7+
(errors.AuthenticationRequired, "Authentication required"),
8+
(errors.BackendNotAvailable, "Backend not available"),
9+
(errors.BackendTimeout, "Backend timed out"),
10+
(errors.BackendError, "Backend error"),
11+
(errors.UnknownBackendResponse, "Backend responded in unknown way"),
12+
(errors.TooManyRequests, "Too many requests. Try again later"),
13+
(errors.InvalidCredentials, "Invalid credentials"),
14+
(errors.NetworkError, "Network error"),
15+
(errors.ProtocolError, "Protocol error"),
16+
(errors.TemporaryBlocked, "Temporary blocked"),
17+
(errors.Banned, "Banned"),
18+
(errors.AccessDenied, "Access denied"),
19+
(errors.FailedParsingManifest, "Failed parsing manifest"),
20+
(errors.TooManyMessagesSent, "Too many messages sent"),
21+
(errors.IncoherentLastMessage, "Different last message id on backend"),
22+
(errors.MessageNotFound, "Message not found"),
23+
(errors.ImportInProgress, "Import already in progress"),
24+
(jsonrpc.UnknownError, "Unknown error"),
25+
(jsonrpc.ParseError, "Parse error"),
26+
(jsonrpc.InvalidRequest, "Invalid Request"),
27+
(jsonrpc.MethodNotFound, "Method not found"),
28+
(jsonrpc.InvalidParams, "Invalid params"),
29+
(jsonrpc.Timeout, "Method timed out"),
30+
(jsonrpc.Aborted, "Method aborted"),
31+
])
32+
def test_error_default_message(error, expected_error_msg):
33+
error_json = error().json()
34+
35+
assert error_json["message"] == expected_error_msg
36+
37+
38+
@pytest.mark.parametrize("error", [
39+
errors.AuthenticationRequired,
40+
errors.BackendNotAvailable,
41+
errors.BackendTimeout,
42+
errors.BackendError,
43+
errors.UnknownBackendResponse,
44+
errors.TooManyRequests,
45+
errors.InvalidCredentials,
46+
errors.NetworkError,
47+
errors.ProtocolError,
48+
errors.TemporaryBlocked,
49+
errors.Banned,
50+
errors.AccessDenied,
51+
errors.FailedParsingManifest,
52+
errors.TooManyMessagesSent,
53+
errors.IncoherentLastMessage,
54+
errors.MessageNotFound,
55+
errors.ImportInProgress,
56+
jsonrpc.UnknownError,
57+
jsonrpc.ParseError,
58+
jsonrpc.InvalidRequest,
59+
jsonrpc.MethodNotFound,
60+
jsonrpc.InvalidParams,
61+
jsonrpc.Timeout,
62+
jsonrpc.Aborted,
63+
])
64+
def test_set_error_custom_message(error):
65+
custom_message = "test message"
66+
67+
error_json = error(custom_message).json()
68+
69+
assert error_json["message"] == custom_message
70+
71+
72+
@pytest.mark.parametrize("error", [
73+
errors.AuthenticationRequired,
74+
errors.BackendNotAvailable,
75+
errors.BackendTimeout,
76+
errors.BackendError,
77+
errors.UnknownBackendResponse,
78+
errors.TooManyRequests,
79+
errors.InvalidCredentials,
80+
errors.NetworkError,
81+
errors.ProtocolError,
82+
errors.TemporaryBlocked,
83+
errors.Banned,
84+
errors.AccessDenied,
85+
errors.FailedParsingManifest,
86+
errors.TooManyMessagesSent,
87+
errors.IncoherentLastMessage,
88+
errors.MessageNotFound,
89+
errors.ImportInProgress,
90+
jsonrpc.UnknownError,
91+
jsonrpc.ParseError,
92+
jsonrpc.InvalidRequest,
93+
jsonrpc.MethodNotFound,
94+
jsonrpc.InvalidParams,
95+
jsonrpc.Timeout,
96+
jsonrpc.Aborted,
97+
])
98+
def test_set_arbitrary_error_message(error):
99+
arbitrary_messages = [[], {}, (), 1, None]
100+
101+
for msg in arbitrary_messages:
102+
error_json = error(msg).json()
103+
assert error_json["message"] == str(msg)

tests/test_local_size.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async def test_prepare_get_local_size_context_error(plugin, read, write):
122122
request_id = "31415"
123123
error_details = "Unexpected syntax"
124124
error_message, error_code = FailedParsingManifest().message, FailedParsingManifest().code
125-
plugin.prepare_local_size_context.side_effect = FailedParsingManifest(error_details)
125+
plugin.prepare_local_size_context.side_effect = FailedParsingManifest(data=error_details)
126126
request = {
127127
"jsonrpc": "2.0",
128128
"id": request_id,

tests/test_subscriptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ async def test_prepare_get_subscription_games_context_error(plugin, read, write)
271271
request_id = "31415"
272272
error_details = "Unexpected backend error"
273273
error_message, error_code = BackendError().message, BackendError().code
274-
plugin.prepare_subscription_games_context.side_effect = BackendError(error_details)
274+
plugin.prepare_subscription_games_context.side_effect = BackendError(data=error_details)
275275
request = {
276276
"jsonrpc": "2.0",
277277
"id": request_id,

0 commit comments

Comments
 (0)