Skip to content

Fix Result[T] status code inconsistency when both Code and Err are set#3

Draft
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-framework-bugs
Draft

Fix Result[T] status code inconsistency when both Code and Err are set#3
Copilot wants to merge 5 commits into
mainfrom
copilot/fix-framework-bugs

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 5, 2025

When Result[T] has both Code and Err fields set, the HTTP status and error JSON body showed different codes:

// Before: HTTP 403, but body shows {"code":500,"error":"internal_error"}
return Result[User]{
    Code: 403,
    Err:  errors.New("access denied"),
}

Changes

  • Reordered handleResult: Check for errors before writing status code to prevent double-write
  • Added handleErrorWithCode: New function accepts explicit status code alongside error
  • Status code validation: Validates codes are in 100-599 range, logs warning and falls back to inferred code if invalid
  • Documentation: Added function comments explaining the delegation pattern
// After: Both HTTP status and JSON body show 403
func handleResult(w http.ResponseWriter, result Result[any]) error {
    if result.Headers != nil {
        WriteHeaders(w, result.Headers)
    }

    if result.Err != nil {
        if result.Code != 0 {
            return handleErrorWithCode(w, result.Err, result.Code)
        }
        return handleError(w, result.Err)
    }

    if result.Code != 0 {
        w.WriteHeader(result.Code)
    }

    return handleCommonTypes(w, result.Data)
}

Test Coverage

Added 7 test cases covering:

  • Code + Err (explicit code used)
  • Err only (code inferred)
  • Code + Data (normal success)
  • Invalid codes (validation + fallback)
Original prompt

分析该框架有没有bug,若有请修复,并提供对应的测试用例,提交前,请确保用例全部pass


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits November 5, 2025 09:45
Co-authored-by: cymoo <7800791+cymoo@users.noreply.github.com>
Co-authored-by: cymoo <7800791+cymoo@users.noreply.github.com>
Co-authored-by: cymoo <7800791+cymoo@users.noreply.github.com>
Co-authored-by: cymoo <7800791+cymoo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bugs in the framework and add test cases Fix Result[T] status code inconsistency when both Code and Err are set Nov 5, 2025
Copilot AI requested a review from cymoo November 5, 2025 09:58
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.

2 participants