Skip to content

feat(backend): add typed error classes and global error middleware - #621

Open
woahwhattheheck wants to merge 18 commits into
Protocol-Guild:mainfrom
woahwhattheheck:wire/payd-550-error-middleware
Open

woahwhattheheck wants to merge 18 commits into
Protocol-Guild:mainfrom
woahwhattheheck:wire/payd-550-error-middleware

Conversation

@woahwhattheheck

Copy link
Copy Markdown

Closes #550

What

Adds typed application error classes (AppError, NotFoundError, ValidationError, AuthError) and a global Express error middleware that returns a consistent JSON shape:

{ "error": "NotFoundError", "message": "...", "code": "NOT_FOUND", "requestId": "..." }

The previous inline 404/500 handlers are replaced so all unhandled errors share one format. Stack traces are attached only when NODE_ENV=development.

Why

Controllers currently mix ad-hoc status codes and payload shapes. A shared error type plus one middleware makes frontend handling reliable and keeps internal details out of production responses.

How tested

  • Unit tests in backend/src/middleware/__tests__/errorHandler.test.ts (7 cases): typed 404/400/401/403 mapping, production redaction of unknown errors, development stack inclusion, and 404 fallback wiring.
  • npm test -- --testPathPatterns=errorHandler — all passed.

woahwhattheheck and others added 5 commits September 24, 2026 15:36
Introduce AppError, NotFoundError, ValidationError, and AuthError with a
consistent { error, message, code, requestId } response shape. Stack traces
are included only in development.

Closes Protocol-Guild#550
GitHub Actions rejects unknown workflow keys, so these files failed with zero jobs.
GitHub Actions rejects retention-days as a workflow root key, so every
check on this branch failed at parse time. Artifact retention stays on
upload-artifact steps where that key is valid.
GitHub Actions rejects retention-days at the workflow root, so every
check on this branch failed before any job started.
Normalize malformed and oversized request bodies without exposing parser input. Keep request IDs, typed application errors, and development-only stacks intact.
Keep unsupported charset/encoding responses at 415 and form parameter overflow at 413, with constant sanitized messages. Add five actual Express parser regressions to the existing error-handler suite.
Correct the README's universal two-field error claim. Document the typed
handler envelope, recognized parser status/code mappings, request IDs,
environment-dependent stacks and direct payroll response boundary.

Only the Error Handling subsection changes. Current source and existing
cases were read; no runtime, build or maintained checks were replayed.
Forward service failures from all twelve payroll handlers to the existing global error boundary. Use ValidationError for seven missing-query paths and NotFoundError for missing transactions. Keep route/authentication order, service arguments and successful response bodies unchanged.

Focused source-level before/after check: 12 preserved successes/service calls, 12 forwarded service failures, 7 validation mappings, 1 not-found mapping and 2 preserved typed errors. Executed actual TypeScript handlers and AppError classes with Express registration, services, authentication and logger collaborators stubbed; not a full application, HTTP or database test.
Reuse the completed CI repair from 0418d00. Remove unsupported workflow-root retention settings, preserve Scaffold failure through tee with explicit Bash, restore the exact executable placeholder checker from 61311eb, and include checker/workflow edits in the existing secrets-check triggers.

Recipient build 5202999, secrets workflow b8d3b7e, and placeholder manifest f4bdde8 match the previously executed repair inputs. Reuse that evidence; no application build, new suite, hosted execution, release or deployment is asserted. All application and documentation source is unchanged.
Keep the original error-response contract for non-Error thrown values whose string conversion fails. Add focused regression rows to the maintained middleware suite and record the direct-handler execution and its limits.

Compose on the existing CI-repair successor without changing its workflows or executable checker.
Keep operational client messages and development diagnostics unchanged.
Add focused cases for server and non-operational error response messages.
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.

Add proper error handling middleware with typed error classes

1 participant