marshmallow-code / marshmallow-code/flask-smorest

Improved abort method

Open
#309 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Currently flask-smorest republishes the `abort`-method of Flask-Restful, which accepts additional keyword-args and an exception.
However, in `ErrorHandlerMixin.handle_http_exception` only `message`, `errors` and `headers` are supported as keyword-args. Everything else is ignored, which is quite confusing behaviour.

A flask-smorest specific `abort` like this would avoid confusion and allow proper type hints:
```python
def abort(http_status_code: int,
message: typing.Optional[str] = None,
errors: typing.Optional[typing.Mapping] = None,
headers: typing.Optional[typing.Mapping] = None):
"""
Raise an HTTPException for the given http_status_code.
:param http_status_code: the HTTP status code
:param message: an error message
:param errors: can be passed to define the location(s) of the error(s)
:param headers: additional headers to be passed to the response
"""
try:
flask.abort(http_status_code)
except HTTPException as err:
# re-raise exception with additional information
data = {}
if message is not None:
data["message"] = message
if errors is not None:
data["errors"] = errors
if headers is not None:
data["headers"] = headers
err.data = data
raise err
```

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 at ErrorHandlerMixin.handle_http_exception and review how flask-smorest currently republishes Flask-Restful's abort method. Done means providing a flask-smorest-specific abort entry point that accepts the documented status, message, errors, and headers arguments, preserves them on the raised HTTP exception, and supports proper type hints.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.