Skip to content

Commit b824b4f

Browse files
committed
Ensure error handling logic is displaying the issue on the UI
1 parent 83960b0 commit b824b4f

File tree

4 files changed

+38
-37
lines changed

4 files changed

+38
-37
lines changed

api/src/routes/errors.py

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,41 @@ def register_handlers(app: Flask):
6666
def handler_validationerror(e):
6767
return make_response(jsonify({"message": e.messages}), HTTPStatus.BAD_REQUEST)
6868

69-
70-
# 400
71-
class BadRequest(Exception):
69+
class ExceptionWithMessage(Exception):
7270
def __init__(self, message: str = None):
7371
self.message = message
7472

7573
@staticmethod
76-
def handler(e):
77-
if isinstance(e, BadRequest) and e.message is not None:
78-
return make_response(jsonify({"error": e.message}), HTTPStatus.BAD_REQUEST)
79-
return Response(status=HTTPStatus.BAD_REQUEST)
74+
def handler(e, status: HTTPStatus):
75+
if isinstance(e, ExceptionWithMessage) and e.message is not None:
76+
return make_response(jsonify({"error": e.message}), status)
77+
return make_response(status=status)
8078

79+
# 400
80+
class BadRequest(ExceptionWithMessage):
81+
82+
@staticmethod
83+
def handler(e):
84+
return super().handler(e, HTTPStatus.BAD_REQUEST)
8185

8286
# 401
83-
class Unauthorized(Exception):
84-
def __init__(self, message: str = None):
85-
self.message = message
87+
class Unauthorized(ExceptionWithMessage):
8688

8789
@staticmethod
8890
def handler(e):
89-
if isinstance(e, Unauthorized) and e.message is not None:
90-
return make_response(jsonify({"error": e.message}), HTTPStatus.UNAUTHORIZED)
91-
return Response(status=HTTPStatus.UNAUTHORIZED)
91+
return super().handler(e, HTTPStatus.UNAUTHORIZED)
9292

9393

9494
# 404
95-
class NotFound(Exception):
96-
def __init__(self, message: str = None):
97-
self.message = message
95+
class NotFound(ExceptionWithMessage):
9896

9997
@staticmethod
10098
def handler(e):
101-
if isinstance(e, NotFound) and e.message is not None:
102-
return make_response(jsonify({"error": e.message}), HTTPStatus.NOT_FOUND)
103-
return Response(status=HTTPStatus.NOT_FOUND)
104-
99+
return super().handler(e, HTTPStatus.NOT_FOUND)
105100

106101
# 500
107-
class InternalError(Exception):
102+
class InternalError(ExceptionWithMessage):
103+
108104
@staticmethod
109105
def handler(e):
110-
return Response(status=HTTPStatus.INTERNAL_SERVER_ERROR)
106+
return super().handler(e, HTTPStatus.INTERNAL_SERVER_ERROR)

api/src/routes/requests.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ def post(self, *args, **kwargs):
129129
success, status, resp = query_api("POST", "/schedules/", payload=payload)
130130
if not success:
131131
logger.error(f"Unable to create schedule via HTTP {status}: {resp}")
132-
raise InternalError(f"Unable to create schedule via HTTP {status}: {resp}")
132+
logger.debug(resp)
133+
message = f"Unable to create schedule via HTTP {status}: {resp}"
134+
if status == http.HTTPStatus.BAD_REQUEST:
135+
# if Zimfarm replied this is a bad request, then this is most probably
136+
# a bad request due to user input so we can track it like a bad request
137+
raise BadRequest(message)
138+
else:
139+
# otherwise, this is most probably an internal problem in our systems
140+
raise InternalError(message)
133141

134142
# request a task for that newly created schedule
135143
success, status, resp = query_api(
@@ -140,12 +148,12 @@ def post(self, *args, **kwargs):
140148
if not success:
141149
logger.error(f"Unable to request {schedule_name} via HTTP {status}")
142150
logger.debug(resp)
143-
raise InternalError(f"Unable to request schedule via HTTP {status}: {resp}")
151+
raise InternalError(f"Unable to request schedule via HTTP {status}): {resp}")
144152

145153
try:
146154
task_id = resp.get("requested").pop()
147155
if not task_id:
148-
raise ValueError("task_id is False")
156+
raise InternalError("task_id is False")
149157
except Exception as exc:
150158
raise InternalError(f"Couldn't retrieve requested task id: {exc}")
151159

ui/src/components/NewRequest.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@
233233
throw "Didn't receive task_id";
234234
})
235235
.catch(function (error) {
236-
parent.alertError("Unable to create schedule:\n" + Constants.standardHTTPError(error.response));
236+
parent.alertError("Unable to request ZIM creation:<br />" + Constants.standardHTTPError(error.response));
237237
})
238238
.then(function () {
239239
parent.toggleLoader(false);

ui/src/constants.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,16 @@ export default {
9898
599: "Network Connect Timeout Error",
9999
};
100100

101-
if (response === undefined) { // no response
102-
//usually due to browser blocking failed OPTION preflight request
103-
return "Cross-Origin Request Blocked: preflight request failed."
101+
if (response === undefined) {
102+
// no response is usually due to browser blocking failed OPTION preflight request
103+
return "Unknown error, no response ; probably Cross-Origin Request Blocked."
104104
}
105-
let status_text = response.statusText ? response.statusText : statuses[response.status];
106-
if (response.status == 400) {
107-
if (response.data && response.data.error)
108-
status_text += "<br />" + JSON.stringify(response.data.error);
109-
if (response.data && response.data.error_description)
110-
status_text += "<br />" + JSON.stringify(response.data.error_description);
111-
if (response.data && response.data.message)
112-
status_text += "<br />" + JSON.stringify(response.data.message);
105+
// If error is provided, display it (do not display error code since this is too technical)
106+
if (response.data && response.data.error) {
107+
return response.data.error;
113108
}
109+
// Last resort, display only available information
110+
let status_text = response.statusText ? response.statusText : statuses[response.status];
114111
return response.status + ": " + status_text + ".";
115112
},
116113
};

0 commit comments

Comments
 (0)