Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Handle JSON API errors #57

Merged
merged 6 commits into from
Jul 12, 2018
Merged

Handle JSON API errors #57

merged 6 commits into from
Jul 12, 2018

Conversation

juangallostra
Copy link
Contributor

@juangallostra juangallostra commented Jul 12, 2018

Supersedes #52

Description of the issue/feature this PR addresses

Linked issue: #51

Current behavior before PR

When a query to the JSON API for fetching data is not successful (either an error response is received or no response is received at all) then getting the value of the key count from the response raises an error:

number_of_pages = (cd["count"]/effective_window) + 1

number_of_pages = (cd["count"]/effective_window) + 1

Error message:

We’re sorry, but there seems to be an error…

Here is the full error message:

Display traceback as text

Traceback (innermost last):

     Module ZPublisher.Publish, line 138, in publish
     Module ZPublisher.mapply, line 77, in mapply
     Module ZPublisher.Publish, line 48, in call_object
     Module senaite.sync.browser.views, line 95, in __call__
     Module senaite.sync.updatestep, line 34, in run
     Module senaite.sync.updatestep, line 68, in _fetch_data

KeyError: 'count' 

Desired behavior after PR is merged

When receiveing an error response or no response at all log the error and return without raising an exception. Note that we are logging the response error message. We can be sure that if the API response contains an error message the key message will exist due to: https://github.com/senaite/senaite.jsonapi/blob/871959f4b1c9edbb477e9456325527ca78e13ec6/src/senaite/jsonapi/exceptions.py#L11

--
I confirm I have tested this PR thoroughly and coded it according to PEP8
and Plone's Python styleguide standards.

# When we receive an error message in JSON response or we
# don't get any response at all the key 'count' doesn't exist.
if not cd.get("count", None):
error_message = "Error message: {}".format(cd.get('message', "")) if cd.get('message', None) else ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need of that redundancy. The following should be enough:

error_message = "Error message: {}".format(cd.get('message', None) or '')

@juangallostra juangallostra changed the title Handle JSON API errors in _fetch_data Handle JSON API errors Jul 12, 2018
@xispa xispa merged commit 45b4869 into senaite:master Jul 12, 2018
@juangallostra juangallostra deleted the fix-count-key-error branch July 16, 2018 09:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging this pull request may close these issues.

2 participants