From 850f8a05916f750bc2c9e16f62af8e9ee5aa2ae1 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 23 Feb 2026 19:56:05 +0000 Subject: [PATCH] Add ResourceNotFoundException handler for Profile API 404s Add explicit exception handler for ResourceNotFoundException in CustomizeExceptionHandler to return a JSON body with a descriptive error message instead of an empty 404 response. This fixes the generic 'Something went wrong' error shown to users when following profiles with invalid usernames. Also add test coverage for the 404 scenario in ProfileApiTest. MDD-79 Co-Authored-By: unknown <> --- .../api/exception/CustomizeExceptionHandler.java | 8 ++++++++ src/test/java/io/spring/api/ProfileApiTest.java | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/main/java/io/spring/api/exception/CustomizeExceptionHandler.java b/src/main/java/io/spring/api/exception/CustomizeExceptionHandler.java index ade3ff4ff..3ce225055 100644 --- a/src/main/java/io/spring/api/exception/CustomizeExceptionHandler.java +++ b/src/main/java/io/spring/api/exception/CustomizeExceptionHandler.java @@ -1,11 +1,13 @@ package io.spring.api.exception; +import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolationException; @@ -59,6 +61,12 @@ public ResponseEntity handleInvalidAuthentication( }); } + @ExceptionHandler(ResourceNotFoundException.class) + public ResponseEntity handleResourceNotFound( + ResourceNotFoundException e, WebRequest request) { + return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Profile not found")); + } + @Override protected ResponseEntity handleMethodArgumentNotValid( MethodArgumentNotValidException e, diff --git a/src/test/java/io/spring/api/ProfileApiTest.java b/src/test/java/io/spring/api/ProfileApiTest.java index f32091ecd..1087eab50 100644 --- a/src/test/java/io/spring/api/ProfileApiTest.java +++ b/src/test/java/io/spring/api/ProfileApiTest.java @@ -93,4 +93,17 @@ public void should_unfollow_user_success() throws Exception { verify(userRepository).removeRelation(eq(followRelation)); } + + @Test + public void should_return_404_when_following_nonexistent_user() throws Exception { + when(userRepository.findByUsername("nonexistent")).thenReturn(Optional.empty()); + + given() + .header("Authorization", "Token " + token) + .when() + .post("/profiles/nonexistent/follow") + .then() + .statusCode(404) + .body("message", equalTo("Profile not found")); + } }