forked from mtusi2026/mtusi2026lab3
feat(app) lab 3 implemented
This commit is contained in:
@@ -5,25 +5,22 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab2/internal/common"
|
||||
"gitflic.ru/project/bulla-max/mtusi2026lab/internal/common"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == "23505"
|
||||
pgErr, ok := errors.AsType[*pgconn.PgError](err)
|
||||
return ok && pgErr.Code == "23505"
|
||||
}
|
||||
|
||||
// dbError separates a drained dependency (503) from a real bug (500). SQL
|
||||
// details never reach the client; callers only emit the AppError message.
|
||||
func dbError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
switch pgErr.Code {
|
||||
case "23505":
|
||||
return common.Conflict("resource already exists")
|
||||
@@ -32,8 +29,7 @@ func dbError(err error) error {
|
||||
}
|
||||
}
|
||||
|
||||
var netErr net.Error
|
||||
if errors.As(err, &netErr) || errors.Is(err, context.DeadlineExceeded) ||
|
||||
if _, ok := errors.AsType[net.Error](err); ok || errors.Is(err, context.DeadlineExceeded) ||
|
||||
errors.Is(err, context.Canceled) ||
|
||||
pgconn.SafeToRetry(err) ||
|
||||
errors.Is(err, pgx.ErrTxClosed) {
|
||||
|
||||
Reference in New Issue
Block a user