From d16cc65a44684e697bda15d2bdc1aacee4d04e75 Mon Sep 17 00:00:00 2001 From: yash Date: Mon, 22 Jan 2024 18:25:45 +0300 Subject: [PATCH] handling special errors in postgresql --- internal/storage/postgresql/postgresql.go | 13 +++++++++---- internal/storage/storage.go | 3 ++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/storage/postgresql/postgresql.go b/internal/storage/postgresql/postgresql.go index 0de4056..a0fff0e 100644 --- a/internal/storage/postgresql/postgresql.go +++ b/internal/storage/postgresql/postgresql.go @@ -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) } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index a2a6584..ca6a92d 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -5,5 +5,6 @@ import ( ) var ( - ErrNotFound = errors.New("not found") + ErrRecipeNotFound = errors.New("recipe not found") + ErrCategoryNotFound = errors.New("category not found") )