From 4cf2021464dadfa6cafe11fc668353527d6c941c Mon Sep 17 00:00:00 2001 From: Sergio Fenoll Date: Wed, 16 Nov 2022 15:27:38 +0100 Subject: [PATCH 1/4] feat: follow the same JSON:API structure for errors as the JS template In the [JS template](https://github.com/mu-semtech/mu-javascript-template/blob/master/helpers/mu/server.js#L30-L35) errors get a message passed in which is returned as the title member. Note that the JS template should also pass status as a member of the error obejct, as happens here, but it currently doesn't. --- helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers.py b/helpers.py index 8fd2fdd..44cfec6 100644 --- a/helpers.py +++ b/helpers.py @@ -52,7 +52,7 @@ def error(msg, status=400, **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["title"] = msg error_obj["status"] = status response = jsonify({ "errors": [error_obj] From c0d396e5f28d9b399dd77d64d184ac8031b096d3 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Wed, 4 Oct 2023 17:13:20 +0200 Subject: [PATCH 2/4] Specific impplementation for error It seems to make sense to describe the specific model that may be followed because we had to look it up ourselves. Updating the implementation to that. --- README.md | 5 ++--- helpers.py | 19 ++++++++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 83c30e4..8b7349f 100644 --- a/README.md +++ b/README.md @@ -89,9 +89,8 @@ 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) - -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). +#### 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). Allowed keys are described by [JSONAPI error objects](https://jsonapi.org/format/#error-objects). #### query(query) diff --git a/helpers.py b/helpers.py index 44cfec6..dba373a 100644 --- a/helpers.py +++ b/helpers.py @@ -48,12 +48,21 @@ 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): """Returns a Response object containing a JSONAPI compliant error response - with the given status code (400 by default).""" - error_obj = kwargs - error_obj["title"] = 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 + } + 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] }) From 4bcd8d8e1646ddb57dc8227e3d5eec650f11c557 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Thu, 5 Oct 2023 10:07:46 +0200 Subject: [PATCH 3/4] Deprecated support for **kwargs in error helper The error helper now has deprecated support for **kwargs. Any argument that does not belong to the runtime will be warned about. --- helpers.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/helpers.py b/helpers.py index dba373a..a921641 100644 --- a/helpers.py +++ b/helpers.py @@ -48,7 +48,7 @@ def rewrite_url_header(request): return request.headers.get('X-REWRITE-URL') -def error(title, status="400", detail=None, id=None, links=None, code=None, source=None, meta=None): +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). @@ -57,6 +57,14 @@ def error(title, status="400", detail=None, id=None, links=None, code=None, sour "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 From 3330fb3d42c9b96cc12c8df34a90b287ece86fe5 Mon Sep 17 00:00:00 2001 From: Aad Versteden Date: Thu, 5 Oct 2023 10:19:12 +0200 Subject: [PATCH 4/4] Update readme for error helper --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8b7349f..eac11c6 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,10 @@ 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", 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). Allowed keys are described 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)