-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement response plugins to run on all responses, including error c… #7689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
…ases (401, 429). Update `HandleError()` to execute response chain for Tyk errors, ensuring customization of status, body, and headers. Add tests for response chain behavior and recursion guard. Document changes in `plan.md`.
🚨 Jira Linter FailedCommit: The Jira linter failed to validate your PR. Please check the error details below: 🔍 Click to view error detailsNext Steps
This comment will be automatically deleted once the linter passes. |
|
This PR enhances the gateway's response middleware functionality by enabling response plugins to execute on all responses, including gateway-generated errors such as 401 (Unauthorized) and 429 (Rate Limit Exceeded). Previously, these plugins would only run on successful or error responses from an upstream service. Files Changed AnalysisThe changes are concentrated in two files:
Architecture & Impact Assessment
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-01-22T19:52:25.171Z | Triggered by: pr_opened | Commit: 93af6f5 💡 TIP: You can chat with Visor using |
|
API Changes no api changes detected |
Security Issues (1)
Architecture Issues (1)
Performance Issues (1)
Quality Issues (2)
Powered by Visor from Probelabs Last updated: 2026-01-22T19:52:28.090Z | Triggered by: pr_opened | Commit: 93af6f5 💡 TIP: You can chat with Visor using |
Response Plugins Run on All Responses
Problem Statement
Response middleware only executes on successful upstream responses. When Tyk returns errors (401 auth, 429 rate limit, etc.), response middleware is bypassed. Customers want to customize responses on a per-API basis for both success and error cases.
Solution: Response Plugins Always Run
Extend response middleware to run on all responses, not just upstream success. The plugin sees every response and decides what to do based on status code.
One plugin, all responses.
Implementation
Primary change:
gateway/handler_error.goIn
HandleError(), build the error body into a buffer, run the response chain on a constructedhttp.Response, and only then write the response.Key points:
responsewith status, cloned headers, body, and request.handleResponseChainonly when configured and not already in an error-response chain (guard).w.Header()and copying fromresponse.Headerso deletions apply.response.Bodyto the final buffer so analytics can read it.Guard: prevent recursion
When ErrorHandler is invoked from a response hook failure, skip re-running the response chain by setting a request-context flag while the chain is executing.
Edge case:
errCustomBodyResponseKeep existing behavior. GraphQL and similar flows already wrote to the response writer, so there is nothing to intercept.
Tests
gateway/handler_error_test.go: global response header add/remove is applied to gateway errors (ex: 401). Confirms response chain runs and header removals are respected.gateway/handler_error_test.go: response hook failure does not re-enter the response chain. Confirms recursion guard and single response write.Considerations / Follow-ups
response.Bodyis closed after reading from the response chain in case a hook swaps in a streaming body.errCustomBodyResponsepath to confirm it bypasses the response chain as intended.Summary
Response plugins become universal response interceptors with safe recursion guards.