Skip to content

Commit

Permalink
Add more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed May 19, 2024
1 parent 63a8bc1 commit 8f08988
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions tesla_fleet_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ class MobileAccessDisabled(TeslaFleetError):
key = "mobile_access_disabled"


class MissingToken(TeslaFleetError): # Teslemetry specific
"""Teslemetry specific error when no access token is provided."""

message = "Missing access token."
status = 401
key = "missing_token"


class InvalidToken(TeslaFleetError): # Teslemetry specific
"""Teslemetry specific error for invalid access token."""

Expand All @@ -94,7 +102,7 @@ class OAuthExpired(TeslaFleetError):
key = "token expired (401)"


class LoginRequired(TeslaFleetError):
class LoginRequired(TeslaFleetError): # Native and Teslemetry
"""The user has reset their password and a new auth code is required, or the refresh_token has already been used."""

message = "The user has reset their password and a new auth code is required, or the refresh_token has already been used."
Expand Down Expand Up @@ -140,6 +148,14 @@ class NotFound(TeslaFleetError):
status = 404


class InvalidMethod(TeslaFleetError):
"""The HTTP method is not allowed."""

message = "The HTTP method is not allowed."
status = 405
key = "invalid_method"


class NotAllowed(TeslaFleetError):
"""The operation is not allowed."""

Expand Down Expand Up @@ -191,6 +207,13 @@ class Locked(TeslaFleetError):
status = 423


class InvalidResponse(TeslaFleetError):
"""The response from Tesla was invalid."""

message = "The response from Tesla was invalid."
status = 424


class RateLimited(TeslaFleetError):
"""Account or server is rate limited."""

Expand Down Expand Up @@ -274,10 +297,15 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
raise InvalidRequest(data)
elif resp.status == 401:
if error:
for exception in [OAuthExpired, MobileAccessDisabled, LoginRequired]:
for exception in [
OAuthExpired,
MobileAccessDisabled,
LoginRequired,
MissingToken,
InvalidToken,
]:
if error == exception.key:
raise exception(data)
raise InvalidToken(data)
# This error does not return a body
raise OAuthExpired()
elif resp.status == 402:
Expand All @@ -291,6 +319,8 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
elif resp.status == 404:
raise NotFound(data)
elif resp.status == 405:
if error == InvalidMethod.key:
raise InvalidMethod(data)
raise NotAllowed(data)
elif resp.status == 406:
raise NotAcceptable(data)
Expand All @@ -304,6 +334,8 @@ async def raise_for_status(resp: aiohttp.ClientResponse) -> None:
raise InvalidResource(data)
elif resp.status == 423:
raise Locked(data)
elif resp.status == 424:
raise InvalidResponse(data)
elif resp.status == 429:
raise RateLimited(data)
elif resp.status == 451:
Expand Down

0 comments on commit 8f08988

Please sign in to comment.