Files
coder/coderd/aibridged/proto/errors.go
T
Sas SwartandClaude Opus 5.5 1ec94c74dc fix(coderd): return 500, not 403, when AI Gateway authorization hits a database error (#31617)
When coderd's `IsAuthorized` hits a database error, the AI Gateway
answers 403. A coderd outage therefore looks to clients like a
permission problem. The gateway can't tell coderd's own failures from
rejected requests, because coderd returns both as plain errors. Worse,
the API key and user lookups turn any database error into
`ErrUnknownKey` or `ErrUnknownUser`.

coderd now marks each database failure in `IsAuthorized` with the dRPC
error code `proto.ErrCodeInternal`, where the failure happens. This
covers the key, user, workspace, organization membership and effective
group lookups. The key and user lookups return `ErrUnknownKey` and
`ErrUnknownUser` only for `sql.ErrNoRows`. Rejections carry no code. The
gateway answers a coded error with 500 and every other error with 403.

dRPC sends the code with the error, so the protocol version doesn't
change. A gateway and coderd on different versions keep today's 403,
because the older side neither sets nor reads the code.

A connection that drops during the `IsAuthorized` call still gets a 403.
That's left for a follow-up.

`TestAuthorization_InternalErrors` covers each database failure and each
rejection on the direct, workspace token and delegated paths.
`TestServeHTTP_FailureModes` covers the gateway's status codes.

Refs AIGOV-737

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 07:39:54 +02:00

14 lines
589 B
Go

package proto
import "google.golang.org/grpc/codes"
// ErrCodeInternal is the dRPC error code (see drpcerr) that coderd sets on an
// IsAuthorized error caused by coderd itself, such as a failed database query,
// rather than by the request. A gateway answers it with a 500. An error
// without it is a rejection and is answered with a 403, which is also how a
// gateway treats every error from a coderd that does not set the code.
//
// The code travels with the error, not in a message, so it needs no change to
// the protocol version.
const ErrCodeInternal = uint64(codes.Internal)