Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 19, 2024
1 parent f8ebb6a commit ecf0693
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="tesla_fleet_api",
version="0.4.3",
version="0.4.4",
author="Brett Adams",
author_email="hello@teslemetry.com",
description="Tesla Fleet API library for Python",
Expand Down
37 changes: 21 additions & 16 deletions tesla_fleet_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,39 +221,35 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
# This error does not return a body
raise OAuthExpired()

if resp.content_type != "application/json":
text = await resp.text()
return ResponseError({"error": text, "status": resp.status})

data = await resp.json()

try:
resp.raise_for_status()
except aiohttp.ClientResponseError as e:
if resp.content_type != "application/json":
data = await resp.json()
else:
data = {}
if resp.status == 400:
error = data.get("error")
if error == Error.INVALID_COMMAND:
raise InvalidCommand(data) from e
elif error == Error.INVALID_FIELD:
if error == Error.INVALID_FIELD:
raise InvalidField(data) from e
elif error == Error.INVALID_REQUEST:
if error == Error.INVALID_REQUEST:
raise InvalidRequest(data) from e
elif error == Error.INVALID_AUTH_CODE:
if error == Error.INVALID_AUTH_CODE:
raise InvalidAuthCode(data) from e
elif error == Error.INVALID_REDIRECT_URL:
if error == Error.INVALID_REDIRECT_URL:
raise InvalidRedirectUrl(data) from e
elif error == Error.UNAUTHORIZED_CLIENT:
if error == Error.UNAUTHORIZED_CLIENT:
raise UnauthorizedClient(data) from e
else:
raise InvalidRequest({error: e.message}) from e
raise InvalidRequest({error: e.message}) from e
elif resp.status == 401:
error = data.get("error")
if error == Error.TOKEN_EXPIRED:
raise OAuthExpired(data) from e
elif error == Error.MOBILE_ACCESS_DISABLED:
if error == Error.MOBILE_ACCESS_DISABLED:
raise MobileAccessDisabled(data) from e
else:
raise InvalidToken({error: e.message}) from e
raise InvalidToken({error: e.message}) from e
elif resp.status == 402:
raise PaymentRequired(data) from e
elif resp.status == 403:
Expand Down Expand Up @@ -289,3 +285,12 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
elif resp.status == 540:
raise DeviceUnexpectedResponse(data) from e
raise e
finally:
if resp.content_type != "application/json":
raise ResponseError(
{
"status": resp.status,
"error": "Invalid response from Tesla",
"error_description": await resp.text(),
}
)

0 comments on commit ecf0693

Please sign in to comment.