From c89524a98930d7155ac570d9f201abbe724311fc Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Wed, 31 Jul 2024 13:01:22 -0500 Subject: [PATCH] Added HttpStatuses to RecipeController exception handler. --- .../app/mealsmadeeasy/api/recipe/RecipeController.java | 7 ++++++- .../java/app/mealsmadeeasy/api/recipe/RecipeException.java | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java index 1910d5c..a12a37c 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeController.java @@ -6,6 +6,7 @@ import app.mealsmadeeasy.api.recipe.view.RecipeInfoView; import app.mealsmadeeasy.api.user.User; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; @@ -25,7 +26,11 @@ public class RecipeController { @ExceptionHandler public ResponseEntity onRecipeException(RecipeException recipeException) { - return ResponseEntity.badRequest().body(new RecipeExceptionView( + final HttpStatus status = switch (recipeException.getType()) { + case INVALID_ID, INVALID_USERNAME_OR_SLUG -> HttpStatus.NOT_FOUND; + case INVALID_COMMENT_ID -> HttpStatus.BAD_REQUEST; + }; + return ResponseEntity.status(status.value()).body(new RecipeExceptionView( recipeException.getType().toString(), recipeException.getMessage() )); diff --git a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java index c3c5625..25c5c85 100644 --- a/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java +++ b/src/main/java/app/mealsmadeeasy/api/recipe/RecipeException.java @@ -3,7 +3,9 @@ package app.mealsmadeeasy.api.recipe; public class RecipeException extends Exception { public enum Type { - INVALID_OWNER_USERNAME, INVALID_STAR, NOT_VIEWABLE, INVALID_COMMENT_ID, INVALID_USERNAME_OR_SLUG, INVALID_ID + INVALID_USERNAME_OR_SLUG, + INVALID_ID, + INVALID_COMMENT_ID } private final Type type;