aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/utils/services.py30
1 files changed, 24 insertions, 6 deletions
diff --git a/bot/utils/services.py b/bot/utils/services.py
index 439c8d500..c10f0cd46 100644
--- a/bot/utils/services.py
+++ b/bot/utils/services.py
@@ -1,5 +1,3 @@
-from typing import Optional
-
from aiohttp import ClientConnectorError
import bot
@@ -9,18 +7,36 @@ from bot.log import get_logger
log = get_logger(__name__)
FAILED_REQUEST_ATTEMPTS = 3
+MAX_PASTE_LENGTH = 100_000
+
+
+class PasteUploadError(Exception):
+ """Raised when an error is encountered uploading to the paste service."""
+
+
+class PasteTooLongError(Exception):
+ """Raised when content is too large to upload to the paste service."""
-async def send_to_paste_service(contents: str, *, extension: str = "") -> Optional[str]:
+async def send_to_paste_service(contents: str, *, extension: str = "", max_length: int = MAX_PASTE_LENGTH) -> str:
"""
Upload `contents` to the paste service.
- `extension` is added to the output URL
+ `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.
- When an error occurs, `None` is returned, otherwise the generated URL with the suffix.
+ Raises `PasteTooLongError` if contents is too long to upload, and `PasteUploadError` if uploading fails.
+
+ Returns the generated URL with the extension.
"""
extension = extension and f".{extension}"
- log.debug(f"Sending contents of size {len(contents.encode())} bytes to paste service.")
+
+ contents_size = len(contents.encode())
+ if contents_size > min(max_length, MAX_PASTE_LENGTH):
+ log.info("Contents too large to send to paste service. ")
+ raise PasteTooLongError(f"Contents of size {contents_size} greater than maximum size {MAX_PASTE_LENGTH}")
+
+ log.debug(f"Sending contents of size {contents_size} bytes to paste service.")
paste_url = URLs.paste_service.format(key="documents")
for attempt in range(1, FAILED_REQUEST_ATTEMPTS + 1):
try:
@@ -59,3 +75,5 @@ async def send_to_paste_service(contents: str, *, extension: str = "") -> Option
f"Got unexpected JSON response from paste service: {response_json}\n"
f"trying again ({attempt}/{FAILED_REQUEST_ATTEMPTS})."
)
+
+ raise PasteUploadError("Failed to upload contents to pastebin")