Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -59,6 +61,12 @@ public ResponseEntity<Object> handleInvalidAuthentication(
});
}

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<Object> handleResourceNotFound(
ResourceNotFoundException e, WebRequest request) {
return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Profile not found"));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Hardcoded "Profile not found" message returned for all ResourceNotFoundException usages, not just profiles

The new @ExceptionHandler for ResourceNotFoundException hardcodes the response body as {"message": "Profile not found"}, but ResourceNotFoundException is thrown across many different API controllers and GraphQL resolvers — not just ProfileApi.

Affected callers and impact

ResourceNotFoundException is thrown from:

  • ArticleApi.java:41,62,78 — when an article is not found by slug
  • CommentsApi.java:46,57,73,84 — when an article or comment is not found
  • ArticleFavoriteApi.java:33,43 — when an article is not found for favoriting
  • ProfileApi.java:34,48,64,66 — when a profile/user is not found
  • Multiple GraphQL resolvers (ArticleMutation, CommentMutation, ArticleDatafetcher, RelationMutation, ProfileDatafetcher, MeDatafetcher)

For example, when a client requests GET /articles/nonexistent-slug, the ArticleApi throws ResourceNotFoundException, and the handler will return:

{"message": "Profile not found"}

instead of something accurate like "Article not found" or a generic "Resource not found".

Impact: All non-profile 404 responses will contain a misleading error message, confusing API consumers and frontend error displays.

Suggested change
return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Profile not found"));
return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Resource not found"));
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException e,
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/io/spring/api/ProfileApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}