Skip to content

Commit

Permalink
Added more exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Mar 28, 2022
1 parent be24955 commit 810d457
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
14 changes: 11 additions & 3 deletions savepagenow/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
from requests.utils import parse_header_links

from .exceptions import (
BadGateway,
BlockedByRobots,
CachedPage,
Forbidden,
TooManyRequests,
UnknownError,
WaybackRuntimeError,
)

Expand Down Expand Up @@ -50,10 +53,15 @@ def capture(
raise WaybackRuntimeError(error_header)

# If it has an error code, raise that
if response.status_code in [403, 502, 520]:
raise WaybackRuntimeError(response.headers)
elif response.status_code == 429:
status_code = response.status_code
if status_code == 403:
raise Forbidden(response.headers)
elif status_code == 429:
raise TooManyRequests(response.headers)
elif status_code == 502:
raise BadGateway(response.headers)
elif status_code == 520:
raise UnknownError(response.headers)

# If there's a content-location header in the response, we will use that.
try:
Expand Down
18 changes: 18 additions & 0 deletions savepagenow/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ class BlockedByRobots(WaybackRuntimeError):
pass


class BadGateway(WaybackRuntimeError):
"""Raised when archive.org when you receive a 502 bad gateway status code in response to your request."""

pass


class Forbidden(WaybackRuntimeError):
"""Raised when archive.org when you receive a 403 forbidden status code in response to your request."""

pass


class TooManyRequests(WaybackRuntimeError):
"""Raised when archive.org when you have exceeded its throttle on request frequency. Slow it down."""

pass


class UnknownError(WaybackRuntimeError):
"""Raised when archive.org when you receive a 520 unknown status code in response to your request."""

pass

0 comments on commit 810d457

Please sign in to comment.