Skip to content

Commit

Permalink
more error handling in check_status
Browse files Browse the repository at this point in the history
  • Loading branch information
ntBre committed Nov 15, 2024
1 parent ccbd4cd commit 5eafd47
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions zenodo_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os

import requests
from requests.exceptions import JSONDecodeError

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand All @@ -21,11 +22,18 @@
HEADERS = {"Authorization": f"Bearer {TOKEN}"}


def check_status(response, expect):
"Helper for checking that ``response.status_code`` matches ``expect``."
assert (
response.status_code == expect
), f"Request failed ({response.status_code}), body: {response.json()}"
def check_status(response, expect, fatal: bool):
"""Helper for checking that ``response.status_code`` matches ``expect``. If
``fatal`` is ``True``, raise an exception if ``expect`` is not matched."""
if response.status_code != expect:
try:
body = response.json()
except JSONDecodeError as e:
body = e
msg = f"Request failed ({response.status_code}), body: {body}"
logger.error(msg)
if fatal:
raise Exception(msg)


def check_api_access(url, headers):
Expand Down

0 comments on commit 5eafd47

Please sign in to comment.