Skip to content
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

feat: follow the same JSON:API structure for errors as the JS template #7

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,11 @@ Validate whether the Content-Type header contains the JSONAPI `content-type`-hea

Validate whether the type specified in the JSONAPI data is equal to the expected type. Returns a 409 otherwise.

#### error(title, status=400, **kwargs)
#### error(title, status="400", detail=None, id=None, links=None, code=None, source=None, meta=None)

Returns a JSONAPI compliant error [Response object](https://flask.palletsprojects.com/en/1.1.x/api/#response-objects) with the given status code (default: 400). `kwargs` can be any other keys supported by [JSONAPI error objects](https://jsonapi.org/format/#error-objects).
Returns a JSONAPI compliant error [Response object](https://flask.palletsprojects.com/en/1.1.x/api/#response-objects) with the given status code (default: 400). Allowed keys are described by [JSONAPI error objects](https://jsonapi.org/format/#error-objects).

Other keywords are accepted and are merged into the error object but support for them is deprecated.

#### query(query)

Expand Down
27 changes: 22 additions & 5 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,29 @@ def rewrite_url_header(request):
return request.headers.get('X-REWRITE-URL')


def error(msg, status=400, **kwargs):
def error(title, status="400", detail=None, id=None, links=None, code=None, source=None, meta=None, **kwargs):
"""Returns a Response object containing a JSONAPI compliant error response
with the given status code (400 by default)."""
error_obj = kwargs
error_obj["detail"] = msg
error_obj["status"] = status
with the given status code (400 by default).

See https://jsonapi.org/format/#error-objects for desired structure."""
error_obj = {
"title": title,
"status": status
}

for key, value in kwargs.items():
print("[DEPRECATION] Supplying args not supported by jsonapi to error helper is deprecated and support will be removed, received {} => {}".format(key, value), flush=True)
error_obj[key] = value

for kwarg, values in kwargs.items():
print( "{} => {}".format( kwarg, values ) )

if detail is not None: error_obj["detail"] = detail
if id is not None: error_obj["id"] = id
if links is not None: error_obj["links"] = links
if code is not None: error_obj["code"] = code
if source is not None: error_obj["source"] = source
if meta is not None: error_obj["meta"] = meta
response = jsonify({
"errors": [error_obj]
})
Expand Down