New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Error handlers respond with json if applicable #442
Conversation
Code Climate has analyzed commit 7a7cc70 and detected 0 issues on this pull request. View more on Code Climate. |
natlas-server/app/errors/handlers.py
Outdated
from app.errors import bp | ||
from app import db | ||
import elasticsearch | ||
import sentry_sdk | ||
|
||
|
||
def json_response(status: int, msg: str) -> str: | ||
err = json.dumps({"status": status, "message": msg}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey sir, not sure if you noticed, but this is technically part of your API contract that you've added here, but what's the forcing function to ensure that you use a consistent format. Can we share an object, method, something to make sure this "API contract schema" is actually enforced and shared?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this is enforced and shared because all of these handlers return build_response, and build_response always calls it the same way. So the keys in the dict will always be the same so long as we exit through an error handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They always call build_response?
natlas/natlas-server/app/api/routes.py
Line 28 in 2d99d2e
{"status": 400, "message": errmsg, "retry": False} |
Or here?
natlas/natlas-server/app/api/routes.py
Line 48 in 2d99d2e
{"status": 400, "message": errmsg, "retry": False} |
Here?
natlas/natlas-server/app/api/routes.py
Line 68 in 2d99d2e
"status": 404, |
Here?
natlas/natlas-server/app/api/routes.py
Line 111 in 2d99d2e
"status": status_code, |
natlas/natlas-server/app/api/routes.py
Line 141 in 2d99d2e
"status": status_code, |
natlas/natlas-server/app/api/routes.py
Line 192 in 2d99d2e
"status": status_code, |
a31d5ff
to
9498b5d
Compare
return {"status": self.status_code, "message": self.message} | ||
|
||
def get_json(self): | ||
return json.dumps(self.get_dict(), sort_keys=True, indent=4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentions in my serialized responses?
Return json by default unless the client explicitly says text/html is what they want.
This actually changes the default behavior of the error handlers to json and then if
text/html
is in theAccept
header then we'll serve up html instead.