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.
[TT-14798] Replace JSEscapeString with json.Marshal for RFC 8259-compliant error responses #7527
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?
[TT-14798] Replace JSEscapeString with json.Marshal for RFC 8259-compliant error responses #7527
Changes from all commits
40efe08File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
There are no files selected for viewing
Check warning on line 162 in gateway/handler_error.go
documentation Issue
Raw output
Check failure on line 163 in gateway/handler_error.go
security Issue
Raw output
Check failure on line 165 in gateway/handler_error.go
Check warning on line 165 in gateway/handler_error.go
performance Issue
Raw output
Check warning on line 167 in gateway/handler_error.go
architecture Issue
Raw output
Separate the JSON response generation from the template-based logic used for XML. Handle the JSON case by marshaling a dedicated error structure or map directly to the HTTP response writer. This avoids using templates for JSON, improves robustness by allowing for proper error handling, and increases code clarity. Example of a cleaner approach: ```go // In HandleError function if contentType == header.ApplicationXML || contentType == header.TextXML { // ... existing template logic for XML } else { // Handle JSON case directly w.Header().Set(header.ContentType, header.ApplicationJSON) w.WriteHeader(errCode) err := json.NewEncoder(w).Encode(map[string]string{"error": errMsg}) if err != nil { log.WithError(err).Error("Failed to write JSON error response") } return // Return to avoid further writes by the template logic } // The rest of the template execution logic would only apply to XML. ```Check warning on line 66 in gateway/handler_error_test.go
performance Issue
Raw output
Refactor the test to perform the setup once before iterating through the test cases. ```go // Suggested refactored structure func TestHandleErrorJSONEscaping(t *testing.T) { // ... test cases definition ... ts := StartTest(nil) defer ts.Close() // Build a properly initialized API spec ts.Gw.BuildAndLoadAPI(func(spec *APISpec) { spec.Proxy.ListenPath = "/" spec.DoNotTrack = true }) // Get the spec from the gateway specs := ts.Gw.apiSpecs if len(specs) == 0 { t.Fatal("no API specs loaded") } spec := specs[0] // Create error handler errorHandler := &ErrorHandler{ BaseMiddleware: &BaseMiddleware{ Spec: spec, Gw: ts.Gw, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { // Create request and response recorder for each test case req := httptest.NewRequest("GET", "/test", nil) req.Header.Set(header.ContentType, header.ApplicationJSON) w := httptest.NewRecorder() // Call HandleError errorHandler.HandleError(w, req, tc.errorMsg, http.StatusBadRequest, true) // Assertions... }) } } ```Uh oh!
There was an error while loading. Please reload this page.