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

error managment #97

Closed
PCarle opened this issue Aug 2, 2021 · 2 comments
Closed

error managment #97

PCarle opened this issue Aug 2, 2021 · 2 comments
Labels
Milestone

Comments

@PCarle
Copy link

PCarle commented Aug 2, 2021

simple code for reproduce error :

from habanero import Crossref

cr = Crossref()
dois= ['10.1609/aimag.v18i1.1276', '10.1109/ICARSC.2014.6849793']
x = cr.works(ids=dois)

produce:

Traceback (most recent call last):
  File "D:\DPRA\CpvAWorkspace\P37_Mupdf16__PdfAnnoteur_V2_postCovid\ReferenceGraphBuilder\test_crossref_on_crash.py", line 8, in <module>
    x = cr.works(ids=dois)
  File "C:\Python37\lib\site-packages\habanero\crossref\crossref.py", line 360, in works
    **kwargs
  File "C:\Python37\lib\site-packages\habanero\request.py", line 128, in request
    raise RequestError(r.status_code, parse_json_err(r))
  File "C:\Python37\lib\site-packages\habanero\habanero_utils.py", line 63, in parse_json_err
    return x.json()["message"][0]["message"]
  File "C:\Python37\lib\site-packages\requests\models.py", line 889, in json
    self.content.decode(encoding), **kwargs
  File "C:\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python37\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

correction proposition (habanero_utils.py):

def is_json(x):
    if (
        re.search("json", x.headers.get("Content-Type")).__class__.__name__
        == "NoneType"
        or x.status_code==404
    ):
        return False
    else:
        return True 
@sckott
Copy link
Owner

sckott commented Aug 6, 2021

Thanks @PCarle !

However, I think the issue is in these .json calls here

return x.json()["message"][0]["message"]
- which is a requests library method. It tries to parse the returned data as if it JSON.

We should probably use a custom method to "safely" try to parse json and fail out with a useful message if not, or somethign else

@sckott sckott added the bug label Oct 31, 2021
@sckott sckott added this to the v0.8 milestone Oct 31, 2021
@sckott sckott closed this as completed in 7205b78 Oct 31, 2021
@sckott
Copy link
Owner

sckott commented Oct 31, 2021

sorry for the delay on this, fix made

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants