Repository navigation
fix(gateways): return 400 for config validation errors on PUT /gateways/{id} (POST/PUT status asymmetry) - #7246
Open
chiragjangiir wants to merge 3 commits into
Open
chiragjangiir wants to merge 3 commits into
chiragjangiir wants to merge 3 commits into
Conversation
chiragjangiir
requested review from
Lang-Akshay,
brian-hussey,
crivetimihai,
ja8zyjits and
msureshkumar88
as code owners
October 15, 2026 17:30
…teways/{id}
Config validation failures inside gateway_service.update_gateway (e.g. a
token-exchange oauth_config missing target_audience) were swallowed by the
final wrap (raise GatewayError with "Failed to update gateway: ..."), so the
PUT router's ValueError branch never matched and clients received a generic
500, while POST /gateways/ returns 400 for the same invalid payload (IBM#7144).
- gateway_service.update_gateway: preserve the original exception as the
GatewayError's cause (raise ... from e) so the router can identify
validation failures without changing the service's documented error
contract.
- PUT /gateways/{id} handler: map a GatewayError whose cause is a ValueError
to 400 "Unable to process input", matching the POST create path. Gateway
errors with a non-validation cause still return 500 (regression-covered);
named gateway sub-errors keep their existing status codes.
- Regression tests: service-level cause-chain test and router-level 400 / 500
branch tests, verified failing before the fix and passing after.
Fixes IBM#7144
Signed-off-by: Chirag Jangir <184008125+chiragjangiir@users.noreply.github.com>
chiragjangiir
force-pushed
the
fix/gateway-update-validation-400
branch
from
October 28, 2026 15:45
7f6f5c1 to
f7ca1bd
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🐛 Bug-fix PR
🔗 Related Issue
Fixes #7144
📝 Summary
What does this PR do and why?
PUT /gateways/{id}returns HTTP 500 for OAuth config-validation errors thatPOST /gateways/correctly reports as HTTP 400. The same invalid payload (e.g.oauth_config.grant_type = "token-exchange"withouttarget_audience) answers with "Unexpected error" on update and "Unable to process input" on create.update_gatewaywraps every unexpected exception in its final handler:The wrapped exception has no
__cause__set, so the PUT router'sisinstance(ex, ValueError) -> 400branch never matches and the request falls through to the generic 500.register_gateway(POST path) re-raisesValueErrorfrom itsexcept* ValueErrorgroup, which is why create behaves correctly.🐞 Root Cause
Two-part asymmetry between the create and update paths:
gateway_service.update_gateway(mcpgateway/services/gateway_service.py, finalexcept Exceptionwrap) swallows config-validation errors — seven distinctValueError-producing validation paths (deprecated password-grant adoption, query-param auth guards,_validate_token_exchange_config,_validate_private_key_jwt_config) — into a plainGatewayErrorwithout exception chaining.mcpgateway/main.pymapsValueErrorto 400 but has no way to detect that aGatewayErroris wrapping one.🔧 What Changed
mcpgateway/services/gateway_service.py—update_gateway's final wrap now chains the original exception (raise GatewayError(...) from e). This preserves the service's documentedGatewayErrorcontract for callers while making the failure type inspectable by the router.mcpgateway/main.py—update_gatewayPUT handler adds a branch (after the more specific gateway sub-error mappings) that returns400 "Unable to process input"when aGatewayError's cause is aValueError. The issue's suggested fix location (the update handler, not the service) is followed.Both places are minimal: the service keeps raising
GatewayError, and the router keeps existing status codes —GatewayNotFoundError(404),GatewayCredentialError(422),GatewayConnectionError(502), conflict errors (409), and genericGatewayError(500) are all unchanged.🧪 Tests and Verification
New regression tests (fail on
main, pass with this patch):tests/unit/mcpgateway/services/test_gateway_service.py::TestGatewayService::test_update_gateway_config_validation_wraps_value_error_as_cause— drives the real validation path (_validate_token_exchange_configraisingPublicValidationErrorfor a token-exchange update withouttarget_audience) and asserts the wrappedGatewayErrorpreserves theValueErrorcause.tests/unit/mcpgateway/test_main_error_handlers.py::TestGatewayUpdateErrorHandlers::test_update_gateway_wrapped_config_validation_error— PUT returns 400 "Unable to process input" for the chained failure.tests/unit/mcpgateway/test_main_error_handlers.py::TestGatewayUpdateErrorHandlers::test_update_gateway_gateway_error_without_validation_cause_still_500— aGatewayErrorwith a non-validation cause must keep returning 500.Exact commands and results (Python 3.12.15, uv 0.13.1, macOS):
pytest ...::test_update_gateway_wrapped_config_validation_error ...::test_update_gateway_config_validation_wraps_value_error_as_cause(fix stashed)__cause__wasNone) ✅ expected failuresuv run pytest tests/unit/mcpgateway/test_main_error_handlers.py -quv run pytest tests/unit/mcpgateway/services/test_gateway_service.py -qgit diff --checkPer make targets (exact commands executed, pinned tool versions per Makefile):
uv tool run isort==6.1.0 --check-only,uv tool run black==26.3.1 --checkon the four touched filesisort/blackchecks identically with my changes stashed (pre-existing drift in untouched import blocks). My hunks are clean; proven by baseline comparison. ✅ but see Notesuv run pytest tests/unit/mcpgateway/test_main_error_handlers.py tests/unit/mcpgateway/services/test_gateway_service.py -q(withPORTset per local shell requirement)make test/make coverageLocal environment:
uv sync --frozen+make ensure-secretsfor.env;PORTdefaulted to0in this shell and must be overridden forSettingsvalidation (environment quirk, unrelated to the patch).📏 Reviewability
triage— issue [BUG]: PUT /gateways/{id} returns 500 for config validation errors that POST /gateways returns 400 for #7144 currently carries thetriagelabel; happy to wait for triage confirmation and can update the checkbox once removed by a maintainer🏷️ Type of Change
✅ Checklist
make black isort pre-commit) — my hunks areblack==26.3.1/isort==6.1.0clean (verified against baseline), but I deliberately did not run repo-wide formatting tools becausemake black/isortwould reformat large untouched parts of these same files (see Notes)📓 Notes
If AI-assistedreviewability item). DCO sign-off carried on the commit.mainstate (changes stashed),isort 6.1.0andblack 26.3.1already flag the same files at the same volume — including large untouched areas (e.g. the isort diff points at import blocks and# First-Partycomment markers far from my patch). I ran the tools pinned in the Makefile and compared baseline-vs-patch to confirm neither check newly degrade with this change. Happy to split the pre-existing formatting churn into a separate PR if maintainers want it — deliberately not bundled to keep review size small.ValueErrorpath throughupdate_gateway, including the token-exchangetoken_urlSSRF rejection. That deny-path (PublicValidationErrorfromSecurityValidator.validate_url) now also returns 400; the chained-cause tests cover the general ValueError mapping, and named gateway sub-errors keep their specific codes. If maintainers prefer the SSRF case to stay a 500, an exclude-guard is trivial to add — happy to adapt.Exceptions is unchanged (except Exceptionremains the fall-through), so "gateway crashed" vs "my payload is invalid" is now distinguishable, as the issue's Expected Behavior section asks.