diff options
author | Frederick Muriuki Muriithi | 2024-01-29 07:13:37 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-01-30 07:18:24 +0300 |
commit | 7b99f911afb40a4c9687d39d5c0e62c12558a0ab (patch) | |
tree | 7a8972e89f9d110aac5dff6d08ce8af8647583ca /gn2 | |
parent | 0bc0bd0673f8c167558b62645cbba652f329ab08 (diff) | |
download | genenetwork2-7b99f911afb40a4c9687d39d5c0e62c12558a0ab.tar.gz |
Error reporting: Pass external error forward to GN2's error handling
Diffstat (limited to 'gn2')
-rw-r--r-- | gn2/wqflask/external_errors.py | 6 | ||||
-rw-r--r-- | gn2/wqflask/oauth2/request_utils.py | 11 |
2 files changed, 14 insertions, 3 deletions
diff --git a/gn2/wqflask/external_errors.py b/gn2/wqflask/external_errors.py index c4e9a2c7..2b1389b9 100644 --- a/gn2/wqflask/external_errors.py +++ b/gn2/wqflask/external_errors.py @@ -7,7 +7,8 @@ class ExternalRequestError(Exception): def __init__(self, externaluri: str, - error: Exception): + error: Exception, + extrainfo: str = ""): """Initialise the error message.""" self.errorid = uuid4() self.error = error @@ -15,4 +16,5 @@ class ExternalRequestError(Exception): super().__init__( f"error-id: {self.errorid}: We got an error of type " f"'{type(error).__name__}' trying to access {externaluri}:\n\n " - f"{''.join(traceback.format_exception(error))}") + f"{''.join(traceback.format_exception(error))} " + + (f"\n\n{extrainfo}" if bool(extrainfo) else "")) diff --git a/gn2/wqflask/oauth2/request_utils.py b/gn2/wqflask/oauth2/request_utils.py index ade0923b..bd98aaf1 100644 --- a/gn2/wqflask/oauth2/request_utils.py +++ b/gn2/wqflask/oauth2/request_utils.py @@ -1,12 +1,15 @@ """General request utilities""" from typing import Optional, Callable from urllib.parse import urljoin, urlparse +from requests.exceptions import JSONDecodeError import simplejson from flask import ( flash, request, url_for, redirect, Response, render_template, current_app as app) +from gn2.wqflask.external_errors import ExternalRequestError + from .client import SCOPE, oauth2_get def authserver_authorise_uri(): @@ -43,7 +46,13 @@ def process_error(error: Response, "error_description": msg, "status_code": error.status_code } - return {**error.json(), "status_code": error.status_code} + try: + return {**error.json(), "status_code": error.status_code} + except JSONDecodeError as exc: + raise ExternalRequestError( + error.url, + exc, + f"Could not parse error record into JSON:\n\n{error.content}") def request_error(response): app.logger.error(f"{response}: {response.url} [{response.status_code}]") |