-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[TT-16767][Global] [Implementation] Centralised Error Overrides Infrastructure #7867
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
908b5e0
init
MFCaballero 00e175c
init
MFCaballero 5f8807f
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero f825b1c
fixes
MFCaballero 3d39188
linters
MFCaballero 6ef8286
Merge branch 'master' into TT-16767
MFCaballero 39d1335
update bench
MFCaballero 87ec5c8
Merge branch 'master' into TT-16767
MFCaballero d97dc15
Merge branch 'master' into TT-16767
edsonmichaque fa9dfdb
Merge branch 'master' into TT-16767
MFCaballero 5cee70f
simplified comments
MFCaballero 05c6909
Apply suggestion from @andyo-tyk
MFCaballero df7e005
Apply suggestions from code review
MFCaballero acf9089
Merge branch 'master' into TT-16767
MFCaballero d98c0cd
refactor: move error overrides types and methods to apidef to avoid i…
vladzabolotnyi b2b0220
unify to explicit config naming status_code
MFCaballero 05eb46b
fix format issues
MFCaballero a1ef045
[TT-16775] Testing Centralised ErrorOverrides Infrastructure (#7878)
MFCaballero 8e3e920
Merge branch 'master' into TT-16767
MFCaballero 6088067
Merge branch 'master' into TT-16767
MFCaballero 9bfdc18
Merge branch 'master' into TT-16767
MFCaballero bf6cc3d
[TT-16772] Implement Upstream Error Response Overrides (#7896)
MFCaballero fca75d0
Merge branch 'master' into TT-16767
MFCaballero 6071b1a
Merge branch 'master' into TT-16767
MFCaballero b259fa8
[TT-16770] Support Template Data & RFC 7807 Context for Validation Er…
buraksezer 36aa606
Merge branch 'master' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero 8842fa5
Merge branch 'TT-16767' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero 9246a6a
Merge branch 'master' into TT-16767
MFCaballero fbe9f8a
fix sonarqube
MFCaballero 41b4354
Merge branch 'TT-16767' of github.com:TykTechnologies/tyk into TT-16767
MFCaballero 75f0afd
Merge branch 'master' into TT-16767
MFCaballero 0e739a5
fix fmt
MFCaballero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package apidef | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/TykTechnologies/tyk/internal/errors" | ||
| "github.com/TykTechnologies/tyk/regexp" | ||
| ) | ||
|
|
||
| // ErrorOverridesMap maps status codes to their override rules. | ||
| type ErrorOverridesMap map[string][]ErrorOverride | ||
|
|
||
| // ErrorOverride combines an optional matcher with its response. | ||
| type ErrorOverride struct { | ||
| // Match contains optional additional matching criteria. | ||
| Match *ErrorMatcher `json:"match,omitempty"` | ||
|
|
||
| // Response defines the response to return when matched. | ||
| Response ErrorResponse `json:"response"` | ||
|
|
||
| // compiledBodyTmpl is the pre-compiled text/template for inline Body. | ||
| compiledBodyTmpl any `json:"-" ignored:"true"` | ||
|
|
||
| // compiledBodyTmplHTML is the pre-compiled html/template for inline Body. | ||
| compiledBodyTmplHTML any `json:"-" ignored:"true"` | ||
| } | ||
|
|
||
| // SetCompiledTemplates stores the pre-compiled templates for inline Body. | ||
| func (e *ErrorOverride) SetCompiledTemplates(textTmpl, htmlTmpl any) { | ||
| e.compiledBodyTmpl = textTmpl | ||
| e.compiledBodyTmplHTML = htmlTmpl | ||
| } | ||
|
|
||
| // GetCompiledTemplate returns the pre-compiled template for the given content type. | ||
| // Returns nil if no inline Body template was compiled (e.g., using file template). | ||
| func (e *ErrorOverride) GetCompiledTemplate(isXML bool) any { | ||
| if isXML { | ||
| return e.compiledBodyTmpl | ||
| } | ||
|
|
||
| return e.compiledBodyTmplHTML | ||
| } | ||
|
|
||
| // HasCompiledTemplate returns true if this override has a pre-compiled inline Body template. | ||
| func (e *ErrorOverride) HasCompiledTemplate() bool { | ||
| return e.compiledBodyTmpl != nil | ||
| } | ||
|
|
||
| // ErrorMatcher defines additional matching criteria for error overrides. | ||
| type ErrorMatcher struct { | ||
| // Flag matches against the error classification flag from the request context. | ||
| Flag errors.ResponseFlag `json:"flag,omitempty"` | ||
|
|
||
| // MessagePattern is a regex pattern to match against the response body. | ||
| MessagePattern string `json:"message_pattern,omitempty"` | ||
|
|
||
| // BodyField is a JSON path (gjson syntax) to extract a value from the response body. | ||
| BodyField string `json:"body_field,omitempty"` | ||
|
|
||
| // BodyValue is the expected value at BodyField for the match to succeed. | ||
| BodyValue string `json:"body_value,omitempty"` | ||
|
|
||
| // CompiledPattern is the pre-compiled regex for MessagePattern. | ||
| CompiledPattern *regexp.Regexp `json:"-" ignored:"true"` | ||
| } | ||
|
|
||
| // Compile compiles the MessagePattern regex if present. | ||
| // Should be called after unmarshaling from JSON or YAML. | ||
| func (m *ErrorMatcher) Compile() error { | ||
| if m.MessagePattern != "" && m.CompiledPattern == nil { | ||
|
Check warning on line 70 in apidef/error_overrides.go
|
||
| re, err := regexp.Compile(m.MessagePattern) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid regex pattern %q: %w", m.MessagePattern, err) | ||
| } | ||
|
|
||
| m.CompiledPattern = re | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // ErrorResponse defines the override response for error overrides. | ||
| type ErrorResponse struct { | ||
| // StatusCode is the HTTP status code to return. | ||
| StatusCode int `json:"status_code"` | ||
|
|
||
| // Body is the HTTP response body (literal or inline template). | ||
| Body string `json:"body,omitempty"` | ||
|
|
||
| // Message is the semantic error message passed to templates as {{.Message}}. | ||
| Message string `json:"message,omitempty"` | ||
|
|
||
| // Template references an error template file in the templates/ directory. | ||
| Template string `json:"template,omitempty"` | ||
|
|
||
| // Headers are HTTP headers to include in the response. | ||
| Headers map[string]string `json:"headers,omitempty"` | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.