Part of duplicate code analysis: #4711
Summary
The server package has a well-consolidated writeErrorResponse / rejectRequest helper in internal/server/http_helpers.go. However, internal/proxy/handler.go defines a separate writeDIFCForbidden helper that partially overlaps with this pattern, and across the codebase there are ~5 inline httputil.WriteJSONResponse calls with map[string]string{"error": ..., "message": ...} that duplicate what writeErrorResponse does.
Duplication Details
Pattern: Repeated inline JSON error response construction
The "message"-only shape in writeDIFCForbidden diverges from the "error" + "message" shape used elsewhere, creating inconsistency in error response format for API consumers.
Impact Analysis
- Maintainability: Any change to the JSON error response shape requires changes in multiple places.
- Bug Risk: Medium — a client parsing error responses will receive inconsistent shapes (
{"message":"..."} from DIFC 403s vs {"error":"...","message":"..."} from all other errors).
- Code Bloat: Minor.
Refactoring Recommendations
- Move
writeErrorResponse to internal/httputil/ so both server and proxy packages can use it.
- Update
writeDIFCForbidden to use the shared helper (adding an "error": "difc_forbidden" field for consistency).
- Estimated effort: 1 hour.
- Benefits: consistent error shape for all API consumers, single place to update response format.
Implementation Checklist
Parent Issue
See parent analysis report: #4711
Related to #4711
Generated by Duplicate Code Detector · ● 1M · ◷
Part of duplicate code analysis: #4711
Summary
The server package has a well-consolidated
writeErrorResponse/rejectRequesthelper ininternal/server/http_helpers.go. However,internal/proxy/handler.godefines a separatewriteDIFCForbiddenhelper that partially overlaps with this pattern, and across the codebase there are ~5 inlinehttputil.WriteJSONResponsecalls withmap[string]string{"error": ..., "message": ...}that duplicate whatwriteErrorResponsedoes.Duplication Details
Pattern: Repeated inline JSON error response construction
Severity: Low
Occurrences: 3+
Locations:
internal/proxy/handler.go:30–33—writeDIFCForbidden()(custom variant, different key"message"vs"error")internal/server/http_helpers.go:51–55—writeErrorResponse()(uses"error"+"message"keys)internal/server/that callhttputil.WriteJSONResponsedirectly with ad-hoc mapsCode Sample (proxy/handler.go):
Code Sample (server/http_helpers.go):
The
"message"-only shape inwriteDIFCForbiddendiverges from the"error"+"message"shape used elsewhere, creating inconsistency in error response format for API consumers.Impact Analysis
{"message":"..."}from DIFC 403s vs{"error":"...","message":"..."}from all other errors).Refactoring Recommendations
writeErrorResponsetointernal/httputil/so bothserverandproxypackages can use it.writeDIFCForbiddento use the shared helper (adding an"error": "difc_forbidden"field for consistency).Implementation Checklist
WriteErrorResponseininternal/httputil/proxy/handler.goandserver/http_helpers.goto use shared helper{"error": ..., "message": ...})Parent Issue
See parent analysis report: #4711
Related to #4711