refactor(server): centralize db-error-to-HTTP mapping with writeDBError#470
Merged
Conversation
Add writeDBError(w, op, repo, err) to internal/server/errors.go with a
single switch covering all 23 db sentinel errors (ErrBranchNotFound,
ErrRepoNotFound, ErrSelfApproval, ErrBranchDraft, ErrOrgNotFound,
ErrInviteExpired, ErrIssueNotFound, etc.). Each case maps to the correct
ErrCode constant and HTTP status, with the default logging at slog.Error
and returning 500.
Replace ~30 repetitive inline switch blocks across 11 handler files with
calls to writeDBError, removing the need to repeat the sentinel→code→status
mapping at each call site. For switches containing non-db cases (service.ErrForbidden,
service.ErrConflict) or custom JSON bodies (ErrMergeConflict, ErrRebaseConflict),
those special cases are handled inline first and the remainder falls through to
writeDBError.
Clean up now-unused imports ("errors", "log/slog", db package) from affected
files.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
writeDBError(w http.ResponseWriter, op, repo string, err error)tointernal/server/errors.go— a single switch over all 23 db sentinel errors mapping each to the correctErrCodeand HTTP status; unrecognised errors log atslog.Errorand return 500switch { case errors.Is(err, db.Err...) }blocks across 11 handler files with singlewriteDBError(...)calls (net: -344 / +125 lines)handleMerge,handleRebase) and for handlers withservice.ErrForbidden/service.ErrConflict, the special case is handled inline first;writeDBErrorhandles everything else"errors","log/slog", db package) from affected filesError mappings added to writeDBError
Branch:
ErrBranchNotFound→404,ErrBranchNotActive→409,ErrBranchExists→409,ErrBranchDraft→409Repo:
ErrRepoNotFound→404,ErrRepoExists→409Org:
ErrOrgNotFound→404,ErrOrgExists→409,ErrOrgHasRepos→409,ErrOrgMemberNotFound→404Role:
ErrRoleNotFound→404Invite:
ErrInviteNotFound→404,ErrInviteExpired→410,ErrInviteAlreadyAccepted→409,ErrEmailMismatch→403Release:
ErrReleaseNotFound→404,ErrReleaseExists→409Other:
ErrSelfApproval→403,ErrCommentNotFound→404,ErrProposalNotFound→404,ErrProposalExists→409,ErrSubscriptionNotFound→404,ErrIssueNotFound→404,ErrIssueCommentNotFound→404Test plan
go build ./...— cleango vet ./...— cleango test ./... -short -count=1— all 24 packages pass;TestDockerSmokeis excluded by-shortand requires GCP credentials (pre-existing infra issue unrelated to this change)Notes
handleDeleteBranchpreviously returned "branch is already merged or abandoned" forErrBranchNotActive;writeDBErrornormalises this to "branch is not active" (matches the db error message and all other call sites)server.go requireRepoReadAccessintentionally left unchanged — it mapsErrRoleNotFoundto 403 (access denied), not 404, which is semantically different fromwriteDBError's mappingTestDockerSmokeinfrastructure issue is worth a separate fix (GCP credentials in CI)🤖 Generated with Claude Code