Skip to content

Don't show raw exception text in flash messages - #183

Merged
pancakereport merged 5 commits into
mainfrom
no-raw-exception-flashes
Nov 18, 2026
Merged

pancakereport merged 5 commits into
mainfrom
no-raw-exception-flashes

Conversation

@pancakereport

@pancakereport pancakereport commented Nov 18, 2026 •

Copy link
Copy Markdown

Closes #180

Handlers in server/views.py used to flash the exception text. For database errors, that text includes the SQL statement, its parameters and connection details, and flashes are stored in the session cookie, which is signed but not encrypted. The messages are now written by us. They name what failed and the exam, room or student involved, and include nothing from the exception.

Changes

  • Check before writing. Creating an exam now checks for an existing exam with the same name first. If one exists, the form comes back filled in with "An exam named 'midterm1' already exists in this offering." Room imports already check this in prepare_room.
  • One way to commit. _commit_or_flash commits, or rolls back and reports why it couldn't. After the rollback it looks for the duplicate the commit clashed with, e.g. one saved by another request meanwhile: an exam, a room with a start time (naming the room it clashes with), or "Already imported: …" for newly imported offerings. If it doesn't find one, the failure is unexpected, so a bug isn't called a duplicate. The handlers use this helper instead of each having its own copy.
  • Messages written for users are kept. DataValidationError and GcpError messages are still shown, prefixed with what failed (for example "Couldn't import room 'Soda 999': …"). This includes the Canvas roster import.
  • Unexpected errors get a reference. _flash_unexpected_error rolls back, logs the traceback with an 8-character reference and flashes "Couldn't delete exam 'Midterm 1' due to an unexpected error (ref a1b2c3d4)." Because of the rollback, a failed query doesn't leave the transaction aborted (on Postgres) for the rest of the request, such as the next room of a master-sheet import.
  • Room files. prepare_seat reports non-numeric coordinates and counts instead of failing unexpectedly, and reads rows that are shorter than the header. prepare_room and the duplicate check share one find_room lookup.
  • Files that aren't UTF-8. Uploaded room, student and layout files are now decoded in one place (decode_csv_file). A file that isn't UTF-8 gets "Save the file as CSV with UTF-8 encoding".
  • The error path doesn't need the database. A commit or rollback expires every loaded object, so redirect URLs and names are read before committing, and a failed commit redirects instead of rendering a page. Before, a lost connection gave a 500 and the message was lost. Room edits also leave a start-time clash to the commit, instead of an autoflush while the seats update.
  • Cleanup. Room imports roll back after a failed commit and no longer try to save a room that failed to build. A leftover print is removed.

Saving a room edit when the seats fail to update was there before this PR, and is filed as #184.

Tests

tests/unit/test_error_messages.py covers every failure branch the PR touches:

  • database errors on each action. Commits fail and so do queries after the rollback, so the error path must not touch the database. Each test checks that the message has a reference and none of the SQL or student data, and one checks that the log has the full error under that reference.
  • duplicates found by the check, duplicates saved by another request meanwhile (including a real one for offerings), other broken constraints, and a failed lookup for the duplicate
  • failures building rooms from a custom or master sheet (including that the next room still imports), editing room seats, and bad numbers or short rows in a room file
  • files that aren't UTF-8, the Canvas roster import, and a failing layout preview

All 718 unit tests pass, and every line this PR changes is covered.

🤖 Generated with Claude Code

Handlers flashed str(e), which for a database error carries the SQL
statement, its parameters (student data during imports) and the
database's host into the session cookie. Now:

- a duplicate exam name is checked before inserting, and a duplicate
  room or exam that slips past the check (IntegrityError) is reported
  in plain terms
- DataValidationError and GcpError messages, written for users, are
  still shown, prefixed with what failed
- anything else is logged with its traceback under a short reference,
  and the flash says what failed and gives the reference

Room imports also roll back after a failed commit, and no longer try
to save a room that failed to build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🚀 Staging Preview Deployed!

URL: https://preview-seating-pr-183-137456085712.us-west1.run.app

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.70%. Comparing base (e8be7a7) to head (0e711c8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #183      +/-   ##
==========================================
+ Coverage   87.35%   92.70%   +5.35%     
==========================================
  Files          36       36              
  Lines        2712     2756      +44     
==========================================
+ Hits         2369     2555     +186     
+ Misses        343      201     -142     
Flag Coverage Δ
e2e 33.57% <8.67%> (-0.07%) ⬇️
unit 92.70% <100.00%> (+5.35%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

pancakereport and others added 4 commits November 18, 2026 11:39
Each handler's failure branch now has a test: database errors on hiding,
creating, editing, toggling and deleting, a duplicate exam or room that
slips past the check, failures building rooms from sheets, and errors
importing from the Canvas roster or previewing a layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rors

- Build redirect URLs before committing: a rollback expires every
  object, so with the database gone the redirect failed with a 500
- Report a non-UTF-8 room or student file plainly, by decoding uploads
  in one place that raises DataValidationError
- Keep DataValidationError messages from the Canvas roster import
- Only call an IntegrityError a duplicate when the duplicate is there
  after the rollback; any other broken constraint is unexpected
- Name the room a room edit clashes with, and leave the clash to the
  commit instead of an autoflush while updating seats
- Check for a duplicate exam inside the error handling, report an
  offering imported meanwhile, and share the room duplicate message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Second review:
- _commit_or_flash commits, or rolls back and reports a duplicate or an
  unexpected error with a reference; the handlers use it instead of
  their own copies of that block
- _flash_unexpected_error rolls back too, so a failed query no longer
  leaves the transaction aborted for the next room of an import, and a
  failed seat update returns instead of committing and reporting twice
- Only newly imported offerings count as duplicates, not hidden ones
  being shown again
- Hiding reports success with the name read before the commit
- After a failed commit, redirect instead of rendering a page that
  needs the database
- One find_room lookup, shared by prepare_room and the duplicate check
- prepare_seat reports non-numeric coordinates and counts, and reads
  rows shorter than the header

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Add imported offerings rather than bulk saving them: a bulk save
  inserts right away, so a course imported by someone else meanwhile
  failed outside the error handling with a 500
- Report a failure loading what deleting an exam or student takes with
  it, instead of a 500
- A room without a start time is never a duplicate: the database lets
  those share a name, so a broken constraint there is unexpected

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pancakereport
pancakereport merged commit 604aac7 into main Nov 18, 2026
13 checks passed
@pancakereport
pancakereport deleted the no-raw-exception-flashes branch November 18, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't show raw exception text in flash messages

1 participant