| |
@@ -6,7 +6,7 @@
|
| |
from functools import wraps
|
| |
from werkzeug.datastructures import ImmutableMultiDict
|
| |
from coprs import app
|
| |
- from coprs.exceptions import CoprHttpException
|
| |
+ from coprs.exceptions import CoprHttpException, ObjectNotFound
|
| |
from coprs.logic.complex_logic import ComplexLogic
|
| |
|
| |
|
| |
@@ -20,25 +20,35 @@
|
| |
DELETE = ["POST", "DELETE"]
|
| |
|
| |
|
| |
- @app.errorhandler(CoprHttpException)
|
| |
- def handle_copr_exception(error):
|
| |
- return handle_api_error(error.message, error.code)
|
| |
+ class APIErrorHandler(object):
|
| |
+ def handle_404(self, error):
|
| |
+ if isinstance(error, ObjectNotFound):
|
| |
+ return self.respond(str(error), 404)
|
| |
+ return self.respond("Such API endpoint doesn't exist", 404)
|
| |
|
| |
+ def handle_403(self, error):
|
| |
+ return self.handle_xxx(error)
|
| |
|
| |
- @app.errorhandler(404)
|
| |
- def page_not_found(error):
|
| |
- return handle_api_error("Such API endpoint doesn't exist", 404)
|
| |
+ def handle_400(self, error):
|
| |
+ return self.handle_xxx(error)
|
| |
|
| |
+ def handle_500(self, error):
|
| |
+ return self.respond("Request wasn't successful, there is probably a bug in the API code.", 500)
|
| |
|
| |
- @app.errorhandler(500)
|
| |
- def page_not_found(error):
|
| |
- return handle_api_error("Request wasn't successful, there is probably a bug in the API code.", 500)
|
| |
+ def handle_xxx(self, error):
|
| |
+ return self.respond(self.message(error), error.code)
|
| |
|
| |
+ def respond(self, message, code):
|
| |
+ response = flask.jsonify(error=message)
|
| |
+ response.status_code = code
|
| |
+ return response
|
| |
|
| |
- def handle_api_error(message, code):
|
| |
- response = flask.jsonify(error=message)
|
| |
- response.status_code = code
|
| |
- return response
|
| |
+ def message(self, error):
|
| |
+ if isinstance(error, CoprHttpException):
|
| |
+ return error.message
|
| |
+ if hasattr(error, "description"):
|
| |
+ return error.description
|
| |
+ return str(error)
|
| |
|
| |
|
| |
def query_params():
|
| |
There are multiple issues with our current implementation
of error handling.
First,
apiv3_ns
defines error handlers on application level,not blueprint level. This causes the #531 issue and results in
showing API errors even for exceptions, that happened in UI.
Also,
coprs_ns
defines error handler for 404, but only for the case,that some object is not found. It doesn't cover the case when
a whole API endpoint is not found. We can't add it there, though.
According to the Flask documentation
http://flask.pocoo.org/docs/1.0/blueprints/#error-handlers
it is not possible to add handlers for 404 and 405 on blueprint level.
That's why it is currently implemented in
misc.py
.That basically means, that our error handlers are inconsistently spread
within the application and we need to have some of them for
apiv3_ns
and
coprs_ns
blueprints and also a conditioned handler for 404 onapplication level. I have had multiple moments of clarity, thinking
that I finally understand how the code works, but I didn't. And then
again and gain, so I decided to rather have all handlers implemented
the same way.