Some checks failed
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Failing after 57s
44 lines
943 B
Go
44 lines
943 B
Go
package core
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"log/slog"
|
|
)
|
|
|
|
var (
|
|
ErrNotFound = errors.New("the value does not exist")
|
|
ErrAlreadyExists = errors.New("row already exists")
|
|
|
|
ErrInternal = errors.New("internal server error")
|
|
ErrUnauthorized = errors.New("you are not authorized to perform this action")
|
|
|
|
ErrBadRequest = errors.New("bad request")
|
|
)
|
|
|
|
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 ErrInternal
|
|
}
|
|
|
|
if r != nil {
|
|
rows, err := r.RowsAffected()
|
|
if err != nil {
|
|
slog.ErrorContext(ctx, "database rows affected", "module", module, "err", err)
|
|
return ErrInternal
|
|
}
|
|
|
|
if rows == 0 {
|
|
slog.InfoContext(ctx, "row not found", "module", module)
|
|
return ErrNotFound
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|