error handling for category not found
This commit is contained in:
parent
0b91ea5ba9
commit
c931100c48
|
@ -2,11 +2,13 @@ package recipes_by_category
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"recipes/internal/domain/models"
|
"recipes/internal/domain/models"
|
||||||
resp "recipes/internal/lib/api/response"
|
resp "recipes/internal/lib/api/response"
|
||||||
"recipes/internal/lib/logger/sl"
|
"recipes/internal/lib/logger/sl"
|
||||||
|
"recipes/internal/storage"
|
||||||
|
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
"github.com/go-chi/render"
|
"github.com/go-chi/render"
|
||||||
|
@ -28,7 +30,7 @@ type RecipesProvider interface {
|
||||||
GetRecipesByCategory(ctx context.Context, offset, limit int, category string) ([]models.Recipe, error)
|
GetRecipesByCategory(ctx context.Context, offset, limit int, category string) ([]models.Recipe, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getRecipesLimit int = 16
|
const GetRecipesLimit int = 16
|
||||||
|
|
||||||
func New(log *slog.Logger, recipesProvider RecipesProvider) http.HandlerFunc {
|
func New(log *slog.Logger, recipesProvider RecipesProvider) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -57,8 +59,13 @@ func New(log *slog.Logger, recipesProvider RecipesProvider) http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// get recipes from storage
|
// get recipes from storage
|
||||||
recipes, err := recipesProvider.GetRecipesByCategory(r.Context(), getRecipesLimit*(int(req.Page)-1), getRecipesLimit, req.Category)
|
recipes, err := recipesProvider.GetRecipesByCategory(r.Context(), GetRecipesLimit*(int(req.Page)-1), GetRecipesLimit, req.Category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if errors.Is(err, storage.ErrCategoryNotFound) {
|
||||||
|
log.Error("failed to get recipes by category from storage", sl.Err(err))
|
||||||
|
render.JSON(w, r, resp.Error("category not found"))
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Error("failed to get recipes from storage", sl.Err(err))
|
log.Error("failed to get recipes from storage", sl.Err(err))
|
||||||
render.JSON(w, r, resp.Error("failed to get recipes"))
|
render.JSON(w, r, resp.Error("failed to get recipes"))
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue