Skip to content

Commit

Permalink
add error handling around token refresh and allow it to sanely pass t…
Browse files Browse the repository at this point in the history
…hrough to login flow
  • Loading branch information
AbstractUmbra committed Jun 30, 2024
1 parent e0a1bf0 commit 2ac03c1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 11 additions & 0 deletions hondana/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
if TYPE_CHECKING:
import aiohttp

from hondana.types_.token import GetTokenPayload

from .types_.errors import ErrorType

__all__ = (
"AuthenticationRequired",
"RefreshTokenFailure",
"UploadInProgress",
"MangaDexServerError",
"APIException",
Expand Down Expand Up @@ -83,6 +86,14 @@ class AuthenticationRequired(Exception):
"""An exception to be raised when authentication is required to use this endpoint."""


class RefreshTokenFailure(Exception):
"""An exception to be raised when trying to use or access the refresh token fails."""

def __init__(self, message: str, response_data: GetTokenPayload | None = None) -> None:
self.message = message
self.data = response_data


class UploadInProgress(Exception):
"""An exception to be raised when an upload in progress is already found for the logged-in user.
Expand Down
16 changes: 13 additions & 3 deletions hondana/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
ReportReason,
ReportStatus,
)
from .errors import APIException, BadRequest, Forbidden, MangaDexServerError, NotFound, Unauthorized
from .errors import APIException, BadRequest, Forbidden, MangaDexServerError, NotFound, RefreshTokenFailure, Unauthorized
from .utils import (
MANGA_TAGS,
MANGADEX_TIME_REGEX,
Expand Down Expand Up @@ -209,7 +209,10 @@ async def refresh(self) -> Self:
async with self._http.request(route.verb, route.url, data=data) as resp:
response_data: token.GetTokenPayload = await resp.json()

self.raw_token = response_data["access_token"]
try:
self.raw_token = response_data["access_token"]
except KeyError as exc:
raise RefreshTokenFailure("Could not access refresh token in the auth payload", response_data) from exc

self._parse()

Expand Down Expand Up @@ -329,7 +332,14 @@ async def get_token(self) -> Token:
and self._auth_token.has_expired()
and (self._auth_token.refresh_token and not self._auth_token.refresh_token.has_expired())
):
return await self._auth_token.refresh()
try:
await self._auth_token.refresh()
except RefreshTokenFailure as exc:
LOGGER.error(
"Failed to refresh token. Will attempt the login flow again. Errored payload:\n%s",
exc.data,
exc_info=exc,
)

route = AuthRoute("POST", "/token")

Expand Down

0 comments on commit 2ac03c1

Please sign in to comment.