fix(workspaces): return 404 for missing workspace files

Draft
PO
3d ago
+63-1

Problem

  • A user who opens a deleted or stale workspace file gets a 500 PREVIEW_LOAD_FAILED instead of a not-found state. Each request also captures an exception in error tracking.
  • requireActiveWorkspaceItemRow throws a plain Error when the row does not exist. The preview and content routes catch only WorkspaceForbiddenError, so the not-found case goes to apiFailure.

Origin

  • Error tracking: issue
  • First signal: 2026-10-13
  • Inbox report: open
  • Likely cause: c869ac8
  • Task started by: auto-start, after the report was rated P3 and ready to fix

Changes

  • A typed WorkspaceItemNotFoundError replaces the plain Error in requireActiveWorkspaceItemRow. The message stays the same, so other callers see no change.
  • The file preview and file content routes map this error to a 404, next to the 403 branch. A 404 does not go through apiFailure, so it captures no exception.
RouteBeforeAfter
GET …/files/$itemId/preview (missing item)500 PREVIEW_LOAD_FAILED + exception404 PREVIEW_NOT_FOUND
GET …/files/$itemId/content (missing item)500 FILE_LOAD_FAILED + exception404 FILE_NOT_FOUND

The 404 codes are the same codes that these routes already return when the stored object is missing.

Testing

  • New workspace-file-routes.test.ts: both routes return 404 and record no operational failure. Without the route change, both tests fail with a 500.
  • vp check (format, lint, types): pass.
  • vp test --run --project=node src/features/workspaces: 81 files, 365 tests pass.
  • Skipped: the workers test project and vp build. The change does not touch Durable Object code or the build.

Agent context

  • The deleted item or stale tab cause is a hypothesis. The event does not show which case it was.
  • Other callers of requireActiveWorkspaceItemRow (server functions, AI tools) still treat the error as a generic failure. I did not change them, to keep this PR focused on the reported routes.

Closes #856
Refs #733


Created with PostHog Desktop from this inbox report.

šŸ¤– Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.