Skip to content

feat: classify an error for a handler with KindOf - #361

Open
lx3133584 wants to merge 1 commit into
skudra-go:mainfrom
lx3133584:fix/343-error-kinds
Open

lx3133584 wants to merge 1 commit into
skudra-go:mainfrom
lx3133584:fix/343-error-kinds

Conversation

@lx3133584

Copy link
Copy Markdown
Contributor

Closes #343.

KindOf answers an ErrorKind for an error, so an HTTP handler picks a status without knowing which of ant's 41 sentinels it got. It reads the driver's SQLSTATE where there is one, then the sentinels a request can cause, and leaves everything else KindInternal: a new sentinel is a loud 500 until it is placed, not a quiet 400 that hides a bug.

The order is the one in the plan: a canceled context, then the statement's SQLSTATE, then ErrNotFound, ErrConcurrency, ErrCursor and ErrPaging and any ValidationError. A foreign key failure goes by the statement — KindInvalid on a write, KindConflict on a delete, and KindConflict when there is no statement to tell. A lock timeout retries with the deadlocks. The kinds are seven-wide, small enough for a switch, and decision 177 records them and amends 45.

Tests: ANT_TEST_DATABASES=postgres go test ./tests/... . passes, make lint has 0 issues. Three mutations — 23505 to unavailable, the ErrNotFound branch removed, the delete check removed — each turn the suite red. Only PostgreSQL ran; the fixture's posts foreign key cascades, so the delete test rebuilds it without a cascade first.

A handler answering 500 for every error turns a bad cursor into a server bug, and one answering 400 for every ant error blames the client for a broken connection. Neither can read the 41 sentinels in errors.go.

KindOf returns an ErrorKind: it reads the SQLSTATE code, then the sentinels a request can cause, and leaves everything else internal, so a new sentinel is a loud 500 until it is placed. A foreign key failure on a delete is a conflict and on a write invalid, and a lock timeout retries. Decision 177 records it and amends decision 45.
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.

A handler can't tell a client's mistake from a bug without knowing every ant error

1 participant