Skip to content

Commit

Permalink
Merge pull request #1510 from burnash/bugfix/handler_errors_in_api_er…
Browse files Browse the repository at this point in the history
…ror_parsing

better handler API error parsing.
  • Loading branch information
lavigne958 authored Sep 13, 2024
2 parents c76ad22 + db59084 commit 6973eef
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion gspread/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import Any, Mapping

from requests import Response
from requests.exceptions import JSONDecodeError


class UnSupportedExportFormat(Exception):
Expand Down Expand Up @@ -40,7 +41,19 @@ class APIError(GSpreadException):
such as when we attempt to retrieve things that don't exist."""

def __init__(self, response: Response):
error = dict(response.json()["error"])
try:
error = response.json()["error"]
except JSONDecodeError:
# in case we failed to parse the error from the API
# build an empty error object to notify the caller
# and keep the exception raise flow running

error = {
"code": -1,
"message": response.text,
"status": "invalid JSON",
}

super().__init__(error)
self.response: Response = response
self.error: Mapping[str, Any] = error
Expand Down

0 comments on commit 6973eef

Please sign in to comment.