From 0bc0bd0673f8c167558b62645cbba652f329ab08 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 29 Jan 2024 06:48:06 +0300 Subject: Create framework for error handling and handle connection errors --- gn2/wqflask/oauth2/client.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'gn2/wqflask/oauth2') diff --git a/gn2/wqflask/oauth2/client.py b/gn2/wqflask/oauth2/client.py index c6a3110b..ed4dbbbf 100644 --- a/gn2/wqflask/oauth2/client.py +++ b/gn2/wqflask/oauth2/client.py @@ -1,4 +1,5 @@ """Common oauth2 client utilities.""" +import uuid import json import requests from typing import Any, Optional @@ -11,6 +12,7 @@ from authlib.integrations.requests_client import OAuth2Session from gn2.wqflask.oauth2 import session from gn2.wqflask.oauth2.checks import user_logged_in +from gn2.wqflask.external_errors import ExternalRequestError SCOPE = ("profile group role resource register-client user masquerade " "introspect migrate-data") @@ -76,10 +78,14 @@ def oauth2_post( def no_token_get(uri_path: str, **kwargs) -> Either: from gn2.utility.tools import AUTH_SERVER_URL - resp = requests.get(urljoin(AUTH_SERVER_URL, uri_path), **kwargs) - if resp.status_code == 200: - return Right(resp.json()) - return Left(resp) + uri = urljoin(AUTH_SERVER_URL, uri_path) + try: + resp = requests.get(uri, **kwargs) + if resp.status_code == 200: + return Right(resp.json()) + return Left(resp) + except requests.exceptions.RequestException as exc: + raise ExternalRequestError(uri, exc) from exc def no_token_post(uri_path: str, **kwargs) -> Either: from gn2.utility.tools import ( @@ -99,11 +105,14 @@ def no_token_post(uri_path: str, **kwargs) -> Either: }, ("data" if bool(data) else "json"): request_data } - resp = requests.post(urljoin(AUTH_SERVER_URL, uri_path), - **new_kwargs) - if resp.status_code == 200: - return Right(resp.json()) - return Left(resp) + try: + resp = requests.post(urljoin(AUTH_SERVER_URL, uri_path), + **new_kwargs) + if resp.status_code == 200: + return Right(resp.json()) + return Left(resp) + except requests.exceptions.RequestException as exc: + raise ExternalRequestError(uri, exc) from exc def post(uri_path: str, **kwargs) -> Either: """ -- cgit v1.2.3