From 3733e8959fb7a3e59e019305a89e7ab45dff881c Mon Sep 17 00:00:00 2001 From: Bianca Yang Date: Fri, 26 Apr 2024 09:52:54 -0700 Subject: [PATCH 1/2] add fallback to res.text --- ee/billing/billing_manager.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ee/billing/billing_manager.py b/ee/billing/billing_manager.py index 53b625dff4256..f564ffb160a03 100644 --- a/ee/billing/billing_manager.py +++ b/ee/billing/billing_manager.py @@ -5,6 +5,7 @@ import requests import structlog from django.utils import timezone +from requests.exceptions import JSONDecodeError from rest_framework.exceptions import NotAuthenticated from sentry_sdk import capture_exception @@ -44,7 +45,11 @@ def build_billing_token(license: License, organization: Organization): def handle_billing_service_error(res: requests.Response, valid_codes=(200, 404, 401)) -> None: if res.status_code not in valid_codes: logger.error(f"Billing service returned bad status code: {res.status_code}, body: {res.text}") - raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", res.json()) + try: + response = res.json() + raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", response) + except JSONDecodeError: + raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", res.text) class BillingManager: From 559f25a06d16c4ad9f8eec99611460aed2e561b2 Mon Sep 17 00:00:00 2001 From: Bianca Yang Date: Fri, 26 Apr 2024 11:16:16 -0700 Subject: [PATCH 2/2] lint --- ee/billing/billing_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ee/billing/billing_manager.py b/ee/billing/billing_manager.py index f564ffb160a03..93eebe19a6d6c 100644 --- a/ee/billing/billing_manager.py +++ b/ee/billing/billing_manager.py @@ -1,3 +1,4 @@ +import json from datetime import datetime, timedelta from typing import Any, Optional, cast @@ -5,7 +6,6 @@ import requests import structlog from django.utils import timezone -from requests.exceptions import JSONDecodeError from rest_framework.exceptions import NotAuthenticated from sentry_sdk import capture_exception @@ -48,7 +48,7 @@ def handle_billing_service_error(res: requests.Response, valid_codes=(200, 404, try: response = res.json() raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", response) - except JSONDecodeError: + except json.decoder.JSONDecodeError: raise Exception(f"Billing service returned bad status code: {res.status_code}", f"body:", res.text)