marshmallow-code / marshmallow-code/flask-smorest

Improving error handlers to satisfy both template and API server use cases

Open
#640 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
717
Forks
77
Avg merge
7h 49m
Merged PRs (30d)
3

Description

So I'm using the flask server for two things.

1. a template-based server-side rendering server. It returns a web page.
2. Building an API server using flask-smorest.

Here I ran into a problem with handling errors. If I wanted the server to handle errors like 404 in the template use case, I could register an error handler function to return the appropriate template. like this:

```python
def _register_error_handlers(app):
for errcode in [401, 403, 404, 500]:
@app.errorhandler(errcode)
def render_error(error):
error_code = getattr(error, "code", 500)
return render_template(f"error_{error_code}.html"), error_code

```

But the problem is that this overrides the error handler defined by flask-smorest. If I throw a 404 error in the API use case (flask_smorest.abort()), it will be caught by the error handler I defined "for templates" and will result in an HTML error, as opposed to the intended JSON response.

It wasn't easy to find a way to handle both of these methods.
Here's what I think is the ideal error handling provided by flask-smoret.

```python
from flask import abort as flask_abort # errors in the default flask
from flask_smorest import abort as api_abort # api errors

# pseudocode...
@blp.response(UserSchema)
def users_api():
# omit
if not user:
api_abort(404) # this returns a 404 JSON response (caught by the error handler currently present in flask-smorest)

@blp.response
def users_view():
# similarly omit
if not user:
flask_abort() # this raises an HttpException from werkzeug.
```

I was thinking that flask-smorest could arbitrarily subclass HTTPException and register it in ErrorHandlerMixin, but I'd like to know what you think about this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading ErrorHandlerMixin and Flask's error-handler behavior, then compare the flask-smorest abort path with Flask's HTTPException path in the issue examples. Determine whether both template and API error responses can be supported without overriding one another, and document the agreed behavior and tests needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.