Skip to content

Only debug log the HTML of failed API requests #2862

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

Merged
merged 1 commit into from
Dec 14, 2024
Merged
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
24 changes: 15 additions & 9 deletions submit/submit
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def error(msg: str) -> NoReturn:
exit(-1)


def print_if_json(text: str):
'''Print the text if it is valid JSON, and ignore otherwise'''
try:
data = json.loads(text)
print(json.dumps(data, indent=2))
except json.decoder.JSONDecodeError:
pass


def read_contests() -> list:
'''Read all contests from the API.

Expand Down Expand Up @@ -202,15 +211,14 @@ def do_api_request(name: str):
except requests.exceptions.RequestException as e:
raise RuntimeError(e)

logging.debug(f"API call '{name}' returned:\n{response.text}")
if response.status_code >= 300:
print(response.text)
print_if_json(response.text)
if response.status_code == 401:
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
else:
raise RuntimeError(f'API request {name} failed (code {response.status_code}).')

logging.debug(f"API call '{name}' returned:\n{response.text}")

return json.loads(response.text)


Expand Down Expand Up @@ -322,11 +330,10 @@ def do_api_print():

response = requests.post(url, data=data, headers=headers)

logging.debug(f"API call 'printing' returned:\n{response.text}")

# The connection worked, but we may have received an HTTP error
logging.debug(f"API printing call returned:\n{response.text}")
if response.status_code >= 300:
print(response.text)
print_if_json(response.text)
if response.status_code == 401:
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
else:
Expand Down Expand Up @@ -367,11 +374,10 @@ def do_api_submit():

response = requests.post(url, data=data, files=files, headers=headers)

logging.debug(f"API call 'submissions' returned:\n{response.text}")

# The connection worked, but we may have received an HTTP error
logging.debug(f"API submitting call returned:\n{response.text}")
if response.status_code >= 300:
print(response.text)
print_if_json(response.text)
if response.status_code == 401:
raise RuntimeError('Authentication failed, please check your DOMjudge credentials in ~/.netrc.')
else:
Expand Down
Loading