ms-tester/internal/storage/errhandling.go

24 lines
684 B
Go
Raw Normal View History

2024-08-17 12:23:35 +00:00
package storage
import (
"errors"
2024-08-20 11:18:23 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/lib"
2024-08-17 12:23:35 +00:00
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
)
2024-08-20 11:18:23 +00:00
func handlePgErr(err error) error {
2024-08-17 12:23:35 +00:00
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
2024-08-22 22:56:03 +00:00
return lib.StorageError(err, lib.ErrUnknown, "unexpected error from postgres")
2024-08-17 12:23:35 +00:00
}
if pgerrcode.IsIntegrityConstraintViolation(pgErr.Code) {
2024-08-22 22:56:03 +00:00
// TODO: probably should specify which constraint
return lib.StorageError(err, lib.ErrConflict, pgErr.Message)
2024-08-17 12:23:35 +00:00
}
2024-08-22 22:56:03 +00:00
if pgerrcode.IsNoData(pgErr.Code) {
return lib.StorageError(err, lib.ErrNotFound, pgErr.Message)
}
return lib.StorageError(err, lib.ErrUnimplemented, "unimplemented error")
2024-08-17 12:23:35 +00:00
}