fix: classify database outages and restore real HTTP fault checks
This commit is contained in:
@@ -25,6 +25,8 @@ func publicFailure(code, message string) error {
|
||||
// Unknown errors receive a fixed response without exposing their details.
|
||||
func publicError(err error) error {
|
||||
switch {
|
||||
case errors.Is(err, service.ErrUnavailable):
|
||||
return publicFailure(codeUnavailable, "dependency unavailable")
|
||||
case errors.Is(err, service.ErrInvalid):
|
||||
return fielderr.WithStatus(err, fielderr.NewStatusCode(codeInvalid), "invalid request")
|
||||
case errors.Is(err, service.ErrUnauthorized):
|
||||
|
||||
@@ -32,3 +32,12 @@ func TestPublicErrorSafety(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestPublicErrorUnavailable keeps dependency details out of the public envelope.
|
||||
func TestPublicErrorUnavailable(t *testing.T) {
|
||||
err := publicError(fmt.Errorf("private database detail: %w", service.ErrUnavailable))
|
||||
response := errorResponse(err, "request")
|
||||
if response.Error.Code != codeUnavailable || response.Error.Message != "dependency unavailable" {
|
||||
t.Fatalf("unexpected response: %+v", response)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user