fix: classify database outages and restore real HTTP fault checks
Application / verify (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
Vladislav Agapov
2026-10-16 10:19:46 +03:00
parent b90581f613
commit 87e1e5d94b
25 changed files with 449 additions and 16 deletions
+3 -3
View File
@@ -43,16 +43,16 @@ func (s *Store) Chats(ctx context.Context, userID uuid.UUID, position dto.Positi
"user": userID, "time": position.CreatedAt, "id": position.ID, "limit": limit,
})
if err != nil {
return nil, err
return nil, normalize(err)
}
defer rows.Close()
chats := make([]dto.ChatDTO, 0)
for rows.Next() {
var chat dto.ChatDTO
if err := rows.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil {
return nil, err
return nil, normalize(err)
}
chats = append(chats, chat)
}
return chats, rows.Err()
return chats, normalize(rows.Err())
}
+52
View File
@@ -0,0 +1,52 @@
package postgres
import (
"context"
"errors"
"fmt"
"io"
"net"
"testing"
"github.com/jackc/pgx/v5/pgconn"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab2/internal/store"
)
// TestNormalizeUnavailable classifies dependency failures while retaining their causes.
func TestNormalizeUnavailable(t *testing.T) {
cases := []struct {
name string
err error
}{
{"deadline", context.DeadlineExceeded},
{"closed stream", io.EOF},
{"truncated stream", io.ErrUnexpectedEOF},
{"network", &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("refused")}},
{"connection exception", &pgconn.PgError{Code: "08006"}},
{"connection limit", &pgconn.PgError{Code: "53300"}},
{"shutdown", &pgconn.PgError{Code: "57P01"}},
{"crash", &pgconn.PgError{Code: "57P02"}},
{"starting", &pgconn.PgError{Code: "57P03"}},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := normalize(fmt.Errorf("operation failed: %w", tc.err))
if !errors.Is(err, store.ErrUnavailable) || !errors.Is(err, tc.err) {
t.Fatalf("failure category or cause lost: %T", tc.err)
}
})
}
}
// TestNormalizeUnexpected preserves programming errors instead of misclassifying them as outages.
func TestNormalizeUnexpected(t *testing.T) {
for _, code := range []string{"42601", "23503", "42P01"} {
t.Run(code, func(t *testing.T) {
err := &pgconn.PgError{Code: code}
if normalize(err) != err {
t.Fatal("unexpected SQL error was reclassified")
}
})
}
}
+3 -3
View File
@@ -35,16 +35,16 @@ func (s *Store) Messages(ctx context.Context, chatID uuid.UUID, position dto.Pos
"chat": chatID, "time": position.CreatedAt, "id": position.ID, "limit": limit,
})
if err != nil {
return nil, err
return nil, normalize(err)
}
defer rows.Close()
messages := make([]dto.MessageDTO, 0)
for rows.Next() {
var message dto.MessageDTO
if err := rows.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt); err != nil {
return nil, err
return nil, normalize(err)
}
messages = append(messages, message)
}
return messages, rows.Err()
return messages, normalize(rows.Err())
}
+1 -1
View File
@@ -32,5 +32,5 @@ func (s *Store) RevokeSession(ctx context.Context, hash string) error {
ctx, cancel := context.WithTimeout(ctx, s.timeout)
defer cancel()
_, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash=@hash`, pgx.StrictNamedArgs{"hash": hash})
return err
return normalize(err)
}
+17 -2
View File
@@ -6,6 +6,9 @@ import (
_ "embed"
"errors"
"fmt"
"io"
"net"
"strings"
"time"
"github.com/jackc/pgx/v5"
@@ -105,8 +108,20 @@ func normalize(err error) error {
if errors.Is(err, pgx.ErrNoRows) {
return store.ErrNotFound
}
if databaseError, ok := errors.AsType[*pgconn.PgError](err); ok && databaseError.Code == "23505" {
return store.ErrConflict
if databaseError, ok := errors.AsType[*pgconn.PgError](err); ok {
if databaseError.Code == "23505" {
return store.ErrConflict
}
if strings.HasPrefix(databaseError.Code, "08") || strings.HasPrefix(databaseError.Code, "53") ||
databaseError.Code == "57P01" || databaseError.Code == "57P02" || databaseError.Code == "57P03" {
return fmt.Errorf("%w: %w", store.ErrUnavailable, err)
}
}
var networkError net.Error
var connectError *pgconn.ConnectError
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) ||
errors.As(err, &networkError) || errors.As(err, &connectError) || pgconn.SafeToRetry(err) {
return fmt.Errorf("%w: %w", store.ErrUnavailable, err)
}
return err
}
+1 -1
View File
@@ -54,5 +54,5 @@ func (s *Store) UsersExist(ctx context.Context, ids []uuid.UUID) (bool, error) {
var exists bool
err := s.pool.QueryRow(ctx, `SELECT NOT EXISTS (SELECT id FROM unnest(@ids::uuid[]) AS requested(id)
WHERE NOT EXISTS (SELECT 1 FROM users WHERE users.id=requested.id))`, pgx.StrictNamedArgs{"ids": ids}).Scan(&exists)
return exists, err
return exists, normalize(err)
}