handling special errors in postgresql

This commit is contained in:
yash 2024-01-22 18:25:45 +03:00
parent 26845b5c82
commit d16cc65a44
2 changed files with 11 additions and 5 deletions

View File

@ -2,9 +2,12 @@ package postgresql
import (
"context"
"errors"
"fmt"
"recipes/internal/domain/models"
"recipes/internal/storage"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
@ -12,10 +15,6 @@ type Storage struct {
db *pgxpool.Pool
}
// TODO
// handling special errors
// AddRecipeInformation
func New(ctx context.Context, user, password, addr, dbname string) (*Storage, error) {
const op = "storage.postgresql.New"
@ -188,6 +187,9 @@ func (s *Storage) GetRecipe(ctx context.Context, r_id uint) (models.Recipe, erro
).Scan(&recipe)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return models.Recipe{}, storage.ErrRecipeNotFound
}
return models.Recipe{}, fmt.Errorf("%s: %w", op, err)
}
@ -327,6 +329,9 @@ func (s *Storage) GetRecipesByCategory(ctx context.Context, offset, limit int, c
category, limit, offset,
)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, storage.ErrCategoryNotFound
}
return nil, fmt.Errorf("%s: %w", op, err)
}

View File

@ -5,5 +5,6 @@ import (
)
var (
ErrNotFound = errors.New("not found")
ErrRecipeNotFound = errors.New("recipe not found")
ErrCategoryNotFound = errors.New("category not found")
)