From 7c5ec52301da8087ac78704e7cf1ee3c4a8472f7 Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Fri, 15 Mar 2024 12:10:17 +0300 Subject: add error handling for invalid tokens --- gn3/llms/client.py | 27 +++++++++++++++++++-------- gn3/llms/errors.py | 9 +++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) (limited to 'gn3/llms') diff --git a/gn3/llms/client.py b/gn3/llms/client.py index 5db19e4..042becd 100644 --- a/gn3/llms/client.py +++ b/gn3/llms/client.py @@ -14,6 +14,8 @@ from requests import Session from requests.adapters import HTTPAdapter from urllib.request import urlretrieve from urllib.parse import quote +from gn3.llms.errors import UnprocessableEntity +from gn3.llms.errors import LLMError basedir = os.path.join(os.path.dirname(__file__)) @@ -116,7 +118,8 @@ class GeneNetworkQAClient(Session): @staticmethod def negative_status_msg(response): - return f"Problems\n\tStatus code => {response.status_code}\n\tReason => {response.reason}" + return f"Error: Status code -{response.status_code}- Reason::{response.reason}" + # return f"Problems\n\tStatus code => {response.status_code}\n\tReason => {response.reason}" def ask(self, exUrl, *args, **kwargs): askUrl = self.BASE_URL + exUrl @@ -142,12 +145,20 @@ class GeneNetworkQAClient(Session): try: response = super().request(method, url, *args, **kwargs) response.raise_for_status() - except requests.exceptions.RequestException as exc: - if exc.response.status_code == 422: - raise UnprocessableEntity(exc.request, exc.response) - # from exc - elif i == max_retries - 1: - raise exc + + except requests.exceptions.HTTPError as error: + if error.response.status_code ==500: + raise LLMError(error.request, error.response, f"Response Error,status_code:{error.response.status_code},Reason: Use of Invalid Token") + elif error.response.status_code ==404: + raise LLMError(error.request,error.response,f"404 Client Error: Not Found for url: {self.BASE_URL}") + raise error + + except requests.exceptions.RequestException as error: + raise error + + + + if response.ok: if method.lower() == "get" and response.json().get("data") is None: time.sleep(retry_delay) @@ -206,4 +217,4 @@ class GeneNetworkQAClient(Session): context = resp_text.get('data', {}).get('context', '') return answer, context else: - return res, "Unfortunately, I have nothing." \ No newline at end of file + return res, "Unfortunately, I have nothing." diff --git a/gn3/llms/errors.py b/gn3/llms/errors.py index 006e034..e9f7c02 100644 --- a/gn3/llms/errors.py +++ b/gn3/llms/errors.py @@ -4,6 +4,7 @@ import json from requests import HTTPError + class UnprocessableEntity(HTTPError): """An HTTP 422 Unprocessable Entity error occurred. @@ -51,7 +52,11 @@ class UnprocessableEntity(HTTPError): error = f"{rq_field} {error}" msg = json.dumps(errors) - super(HTTPError, self).__init__(msg, request=request, response=response) + super(HTTPError, self).__init__( + msg, request=request, response=response) - \ No newline at end of file +class LLMError(HTTPError): + def __init__(self, request, response, msg): + super(HTTPError, self).__init__( + msg, request=request, response=response) -- cgit v1.2.3