diff options
author | 2022-05-29 15:56:46 +0100 | |
---|---|---|
committer | 2022-05-29 15:56:46 +0100 | |
commit | 381dfc1e7fd1849c3381971e9332f2fec20c7a7e (patch) | |
tree | b5f38e4228c8686ab85768cbba8f0b4f51d9effa | |
parent | Ensure error uses correct maximum size (diff) |
Raise ValueError if max_length greater than allowed by paste service
-rw-r--r-- | bot/utils/services.py | 9 | ||||
-rw-r--r-- | tests/bot/utils/test_services.py | 5 |
2 files changed, 11 insertions, 3 deletions
diff --git a/bot/utils/services.py b/bot/utils/services.py index 3a6833e72..82c7d284c 100644 --- a/bot/utils/services.py +++ b/bot/utils/services.py @@ -25,17 +25,20 @@ async def send_to_paste_service(contents: str, *, extension: str = "", max_lengt `extension` is added to the output URL. `max_length` can be used to limit the allowed contents length to lower than the maximum allowed by the paste service. + Raises `ValueError` if `max_length` is greater than the maximum allowed by the paste service. Raises `PasteTooLongError` if contents is too long to upload, and `PasteUploadError` if uploading fails. Returns the generated URL with the extension. """ + if max_length > MAX_PASTE_LENGTH: + raise ValueError(f"`max_length` must not be greater than {MAX_PASTE_LENGTH}") + extension = extension and f".{extension}" - max_size = min(max_length, MAX_PASTE_LENGTH) contents_size = len(contents.encode()) - if contents_size > max_size: + if contents_size > max_length: log.info("Contents too large to send to paste service.") - raise PasteTooLongError(f"Contents of size {contents_size} greater than maximum size {max_size}") + raise PasteTooLongError(f"Contents of size {contents_size} greater than maximum size {max_length}") log.debug(f"Sending contents of size {contents_size} bytes to paste service.") paste_url = URLs.paste_service.format(key="documents") diff --git a/tests/bot/utils/test_services.py b/tests/bot/utils/test_services.py index de166c813..e6b95be8e 100644 --- a/tests/bot/utils/test_services.py +++ b/tests/bot/utils/test_services.py @@ -82,3 +82,8 @@ class PasteTests(unittest.IsolatedAsyncioTestCase): contents = "a" * (MAX_PASTE_LENGTH+1) with self.assertRaises(PasteTooLongError): await send_to_paste_service(contents) + + async def test_raises_on_too_large_max_length(self): + """Ensure ValueError is raised if `max_length` passed is greater than `MAX_PASTE_LENGTH`.""" + with self.assertRaises(ValueError): + await send_to_paste_service("Hello World!", max_length=MAX_PASTE_LENGTH+1) |