Skip to content

proxy: replace http.Error with httputil.WriteErrorResponse for consistent JSON error shape#5819

Merged
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-http-error-response
May 16, 2026
Merged

proxy: replace http.Error with httputil.WriteErrorResponse for consistent JSON error shape#5819
lpcox merged 3 commits into
mainfrom
copilot/duplicate-code-fix-http-error-response

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

internal/proxy/handler.go mixed http.Error (plain-text text/plain responses) with httputil.WriteErrorResponse/WriteJSONResponse (JSON), breaking client-side error handling. The httputil package explicitly documents that both server and proxy packages must use these helpers so API consumers always receive the same {"error":"...","message":"..."} shape.

Changes

  • internal/proxy/handler.go: Replace all 7 http.Error calls with httputil.WriteErrorResponse, assigning appropriate error codes:

    HTTP status error code
    400 Bad Request bad_request
    403 Forbidden forbidden
    502 Bad Gateway bad_gateway
    503 Service Unavailable service_unavailable
    500 Internal Server Error internal_error
// Before — plain text, inconsistent Content-Type
http.Error(w, "upstream request failed", http.StatusBadGateway)

// After — uniform JSON shape across all error paths
httputil.WriteErrorResponse(w, http.StatusBadGateway, "bad_gateway", "upstream request failed")

Copilot AI changed the title [WIP] Fix inconsistent HTTP error response format in handler.go proxy: replace http.Error with httputil.WriteErrorResponse for consistent JSON error shape May 16, 2026
Copilot finished work on behalf of lpcox May 16, 2026 16:43
Copilot AI requested a review from lpcox May 16, 2026 16:43
@lpcox lpcox marked this pull request as ready for review May 16, 2026 16:52
Copilot AI review requested due to automatic review settings May 16, 2026 16:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR standardizes error responses emitted by the proxy handler so clients consistently receive JSON error bodies (via internal/httputil.WriteErrorResponse) instead of a mix of JSON and text/plain from http.Error, aligning proxy error behavior with the rest of the gateway.

Changes:

  • Replaced 7 http.Error call sites in internal/proxy/handler.go with httputil.WriteErrorResponse.
  • Normalized status-to-error-code mappings for common proxy failure paths (400/403/500/502/503).
Show a summary per file
File Description
internal/proxy/handler.go Switches all remaining plain-text http.Error responses to httputil.WriteErrorResponse to ensure a consistent JSON error shape.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread internal/proxy/handler.go
Comment on lines +83 to 85
httputil.WriteErrorResponse(w, http.StatusBadRequest, "bad_request", "failed to read request body")
return
}
@lpcox
Copy link
Copy Markdown
Collaborator

lpcox commented May 16, 2026

@copilot address review feedback

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 16, 2026

@copilot address review feedback

Addressed in 7fbe817. I added regression assertions in internal/proxy/handler_test.go to verify the unknown REST endpoint and upstream network error paths return application/json with the expected {"error","message"} body.

Copilot finished work on behalf of lpcox May 16, 2026 17:02
@lpcox lpcox merged commit 3d55d31 into main May 16, 2026
13 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-fix-http-error-response branch May 16, 2026 17:42
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.

[duplicate-code] Duplicate Code Pattern: Inconsistent HTTP Error Response Format in proxy/handler.go

3 participants