Open koko236 opened 1 week ago
Use abort
.
This should work:
from flask_smorest import abort
from flask_smorest.error_handler import ErrorSchema
@blp.response(200, ProjectSchema)
# I woudn't bother documenting the 404 but if you want to do so, uncomment
# @blp.alt_response(404, schema=ErrorSchema)
def get(self, project_uuid):
'''Get project by uuid'''
project = projects_service.get_project_by_id(project_uuid)
if project is None:
abort(404)
# I'd use the default message but you may override it
# abort(404, message="Project not found")
# ...rest of the function...
Thanks a million! 👍🏿
So just to clarify, as discussed here: (https://github.com/marshmallow-code/flask-smorest/issues/166) the ONLY way to trigger alt response schema is by using abort
method and if we return
response then no matter what HTTP status is set the main response schema will be used, right?
I have endpoints defined with Marshmallow schemas, eg.:
The problem is when I return 404 the json is empty because (I assume) 'message' key gets stripped by ProjectSchema. When I comment out
@blp.response
line I can get the message error as intended. I tried creating ErrorSchema:And alt_response as follows:
But it doesn't help. What is the correct way of handling such a scenario?