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

Return API errors following JSON-API convention #68

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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 tdmq/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ def sources_post():
data = request.json
try:
tdmq_ids = db.load_sources(data)
except tdmq.errors.DuplicateItemException:
raise wex.Conflict()
except tdmq.errors.DuplicateItemException as e:
logger.debug("Duplicate item exception")
logger.debug(e)
raise wex.Conflict("Duplicate source id")
return jsonify(tdmq_ids)


Expand Down
13 changes: 12 additions & 1 deletion tdmq/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,18 @@ def create_app(test_config=None):

@app.errorhandler(wex.HTTPException)
def handle_errors(e):
return jsonify({"error": ERROR_CODES.get(e.code)}), e.code
# adopt the JSON API convention for errors: https://jsonapi.org/examples/#error-objects
error = {
"status": str(e.code),
"source": { "pointer": flask.request.path },
"title": ERROR_CODES.get(e.code),
}
if e.description:
error['detail'] = e.description

response_data = { 'errors' : [ error ] }

return jsonify(response_data), e.code

@app.teardown_appcontext
def teardown_db(arg):
Expand Down
10 changes: 8 additions & 2 deletions tdmq/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import requests
from requests.exceptions import HTTPError

import tiledb
from tdmq.client.sources import NonScalarSource, ScalarSource
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self, tdmq_base_url=None, auth_token=None):
self.tiledb_ctx = None
self.tiledb_vfs = None
self.headers = {'Authorization': f'Bearer {auth_token}'} if auth_token is not None else {}


def requires_connection(func):
"""
Expand Down Expand Up @@ -121,7 +122,12 @@ def register_source(self, definition, nslots=10*24*3600*365):
_logger.error('Failure in creating tiledb array: %s, cleaning up', e)
self._destroy_source(tdmq_id)
raise TdmqError(f"Internal failure in registering {definition.get('id', '(id unavailable)')}.")
return self.get_source(tdmq_id)
try:
return self.get_source(tdmq_id)
except HTTPError as e:
_logger.error("Failed to retrieve newly created source %s", tdmq_id)
raise


@requires_connection
def add_records(self, records):
Expand Down