40 lines
806 B
Go
40 lines
806 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"log/slog"
|
|
"spend-sparrow/internal/types"
|
|
)
|
|
|
|
var (
|
|
ErrNotFound = errors.New("the value does not exist")
|
|
ErrAlreadyExists = errors.New("row already exists")
|
|
)
|
|
|
|
func TransformAndLogDbError(ctx context.Context, module string, r sql.Result, err error) error {
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
slog.ErrorContext(ctx, "database sql", "module", module, "err", err)
|
|
return types.ErrInternal
|
|
}
|
|
|
|
if r != nil {
|
|
rows, err := r.RowsAffected()
|
|
if err != nil {
|
|
slog.ErrorContext(ctx, "database rows affected", "module", module, "err", err)
|
|
return types.ErrInternal
|
|
}
|
|
|
|
if rows == 0 {
|
|
slog.InfoContext(ctx, "row not found", "module", module)
|
|
return ErrNotFound
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|