Skip to content

Fix middleware DB error handling to return proper 503 instead of raw 500 - #5592

Open
WeiQuan0605 wants to merge 3 commits into
mainfrom
fix-middleware-db-error-handling
Open

WeiQuan0605 wants to merge 3 commits into
mainfrom
fix-middleware-db-error-handling

Conversation

@WeiQuan0605

Copy link
Copy Markdown
Contributor
  • A short explanation of the proposed change:
    When the database is unavailable, the CC API was returning a raw 500 Internal Server Error with no JSON body instead of a structured error response. This happened because UserContextSetter middleware runs outside the Rails/Sinatra error handling stack, database errors raised there never reach rescue_from in the application controllers.

    This PR:

    1. Adds rescue Sequel::DatabaseError in UserContextSetter#call, returning a proper 503 Service Unavailable JSON response with X-Vcap-Request-ID header
    2. Fixes a Content-Type: trailing colon typo in SecurityContextSetter's UAA 502 response (the header key was 'Content-Type:' instead of 'Content-Type', causing the header to be silently dropped)
  • An explanation of the use cases your change solves
    During a database failover or outage, clients receive a proper 503 with a structured CF-ServiceUnavailable JSON body and a correlation ID (X-Vcap-Request-ID) instead of a raw 500 with no body

  • I have reviewed the contributing guide

  • I have viewed, signed, and submitted the Contributor License Agreement

  • I have made this pull request to the main branch

  • I have run all the unit tests using bundle exec rake

  • I have run CF Acceptance Tests

* Also fixes a Content-Type: trailing colon typo in SecurityContextSetter's
UAA 502 response.
@WeiQuan0605
WeiQuan0605 marked this pull request as ready for review October 27, 2026 15:30
@WeiQuan0605
WeiQuan0605 requested review from a team October 28, 2026 12:19
Comment thread middleware/user_context_setter.rb Resolved
Addresses reviewer suggestion to follow the pattern in TooManyRequests mixin.
…unner_spec

Puma::Server.new stub was not reliably intercepted when setup_metrics
spawned a Thread, causing an ArgumentError in CI.

This branch has not been deployed

No deployments
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.

2 participants