Skip to content

Commit

Permalink
Sort praw.exceptions.RedditErrorItem.__init__ arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
LilSpazJoekp committed Jan 15, 2022
1 parent 26997b9 commit 61a4a71
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
19 changes: 12 additions & 7 deletions praw/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from typing import List, Optional, Union
from warnings import warn

from .util import _deprecate_args


class PRAWException(Exception):
"""The base PRAW Exception that all other exception classes extend."""
Expand All @@ -28,17 +30,19 @@ def error_message(self) -> str:
error_str += f" on field {self.field!r}"
return error_str

@_deprecate_args("error_type", "message", "field")
def __init__(
self,
error_type: str,
message: Optional[str] = None,
*,
field: Optional[str] = None,
message: Optional[str] = None,
):
"""Initialize a :class:`.RedditErrorItem` instance.
:param error_type: The error type set on Reddit's end.
:param message: The associated message for the error.
:param field: The input field associated with the error, if available.
:param message: The associated message for the error.
"""
self.error_type = error_type
Expand Down Expand Up @@ -84,9 +88,9 @@ def parse_exception_list(exceptions: List[Union[RedditErrorItem, List[str]]]):
exception
if isinstance(exception, RedditErrorItem)
else RedditErrorItem(
exception[0],
exception[1] if bool(exception[1]) else "",
exception[2] if bool(exception[2]) else "",
error_type=exception[0],
field=exception[2] if bool(exception[2]) else "",
message=exception[1] if bool(exception[1]) else "",
)
for exception in exceptions
]
Expand Down Expand Up @@ -228,11 +232,12 @@ class ReadOnlyException(ClientException):
class TooLargeMediaException(ClientException):
"""Indicate exceptions from uploading media that's too large."""

def __init__(self, maximum_size: int, actual: int):
@_deprecate_args("maximum_size", "actual")
def __init__(self, *, actual: int, maximum_size: int):
"""Initialize a :class:`.TooLargeMediaException` instance.
:param maximum_size: The maximum_size size of the uploaded media.
:param actual: The actual size of the uploaded media.
:param maximum_size: The maximum size of the uploaded media.
"""
self.maximum_size = maximum_size
Expand Down
4 changes: 3 additions & 1 deletion praw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ def _parse_xml_response(self, response: "Response"):
if tags[:4] == ["Code", "Message", "ProposedSize", "MaxSizeAllowed"]:
# Returned if image is too big
code, message, actual, maximum_size = [element.text for element in root[:4]]
raise TooLargeMediaException(int(maximum_size), int(actual))
raise TooLargeMediaException(
actual=int(actual), maximum_size=int(maximum_size)
)

def _submit_media(self, data: dict, timeout: int, websocket_url: str = None):
"""Submit and return an `image`, `video`, or `videogif`.
Expand Down
26 changes: 19 additions & 7 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,35 @@ def test_str(self):

class TestRedditErrorItem:
def test_equality(self):
resp = ["BAD_SOMETHING", "invalid something", "some_field"]
error = RedditErrorItem(*resp)
error2 = RedditErrorItem(*resp)
resp = {
"error_type": "BAD_SOMETHING",
"field": "some_field",
"message": "invalid something",
}
error = RedditErrorItem(**resp)
error2 = RedditErrorItem(**resp)
assert error == error2
assert error != 0

def test_property(self):
error = RedditErrorItem("BAD_SOMETHING", "invalid something", "some_field")
error = RedditErrorItem(
"BAD_SOMETHING", field="some_field", message="invalid something"
)
assert (
error.error_message
== "BAD_SOMETHING: 'invalid something' on field 'some_field'"
)

def test_str(self):
error = RedditErrorItem("BAD_SOMETHING", "invalid something", "some_field")
error = RedditErrorItem(
"BAD_SOMETHING", field="some_field", message="invalid something"
)
assert str(error) == "BAD_SOMETHING: 'invalid something' on field 'some_field'"

def test_repr(self):
error = RedditErrorItem("BAD_SOMETHING", "invalid something", "some_field")
error = RedditErrorItem(
"BAD_SOMETHING", field="some_field", message="invalid something"
)
assert (
repr(error)
== "RedditErrorItem(error_type='BAD_SOMETHING', message='invalid something', field='some_field')"
Expand All @@ -68,7 +78,9 @@ def test_items(self):
container = RedditAPIException(
[
["BAD_SOMETHING", "invalid something", "some_field"],
RedditErrorItem("BAD_SOMETHING", "invalid something", "some_field"),
RedditErrorItem(
"BAD_SOMETHING", field="some_field", message="invalid something"
),
]
)
for exception in container.items:
Expand Down

0 comments on commit 61a4a71

Please sign in to comment.