Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send better error messages for 'ValidationError's #225

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/pinnwand/handler/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def post(self) -> None: # type: ignore
log.info(
"CreateAction.post: a paste was submitted with an invalid expiry"
)
raise error.ValidationError()
raise error.ValidationError("Invalid expiry provided")

auto_scale = self.get_body_argument("long", None) is None

Expand All @@ -197,19 +197,23 @@ def post(self) -> None: # type: ignore

if not all([lexers, raws, filenames]):
# Prevent empty argument lists from making it through
raise error.ValidationError()
raise error.ValidationError(
"'lexers', 'raws', and 'filenames' arguments must not be empty"
)

if not all(raw.strip() for raw in raws):
# Prevent empty raws from making it through
raise error.ValidationError()
raise error.ValidationError("Empty pastes are not allowed")

if any(len(L) != len(lexers) for L in [lexers, raws, filenames]):
log.info("CreateAction.post: mismatching argument lists")
raise error.ValidationError()
raise error.ValidationError(
"'lexers', 'raws', and 'filenames' arguments must be the same length"
)

if any(lexer not in utility.list_languages() for lexer in lexers):
log.info("CreateAction.post: a file had an invalid lexer")
raise error.ValidationError()
raise error.ValidationError("Invalid lexer provided")

with database.session() as session, utility.SlugContext(
auto_scale
Expand All @@ -228,9 +232,13 @@ def post(self) -> None: # type: ignore
)
)

if sum(len(f.fmt) for f in paste.files) > configuration.paste_size:
total_size = sum(len(f.fmt) for f in paste.files)
if total_size > configuration.paste_size:
log.info("CreateAction.post: sum of files was too large")
raise error.ValidationError()
raise error.ValidationError(
"Sum of file sizes exceeds size limit when syntax highlighting applied "
f"({total_size//1024}kB > {configuration.paste_size//1024}kB)"
)

# For the first file we will always use the same slug as the paste,
# since slugs are generated to be unique over both pastes and files
Expand Down
Loading