Fix ProfileApi error handling for invalid usernames (MDD-79)#371
Fix ProfileApi error handling for invalid usernames (MDD-79)#371devin-ai-integration[bot] wants to merge 1 commit intomasterfrom
Conversation
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 <>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
| @ExceptionHandler(ResourceNotFoundException.class) | ||
| public ResponseEntity<Object> handleResourceNotFound( | ||
| ResourceNotFoundException e, WebRequest request) { | ||
| return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Profile not found")); |
There was a problem hiding this comment.
🔴 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 slugCommentsApi.java:46,57,73,84— when an article or comment is not foundArticleFavoriteApi.java:33,43— when an article is not found for favoritingProfileApi.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.
| return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Profile not found")); | |
| return ResponseEntity.status(NOT_FOUND).body(Map.of("message", "Resource not found")); |
Was this helpful? React with 👍 or 👎 to provide feedback.
Fix ProfileApi error handling for invalid usernames (MDD-79)
Summary
Adds an explicit
@ExceptionHandlerforResourceNotFoundExceptioninCustomizeExceptionHandlerso that 404 responses include a JSON body ({"message": "Profile not found"}) instead of an empty response. Previously, Spring Boot's default 404 handling kicked in with no body, causing frontends to show a generic "Something went wrong" message.Also adds a test in
ProfileApiTestverifying that following a nonexistent user returns 404 with the expected message.Review & Testing Checklist for Human
"Profile not found"is hardcoded for allResourceNotFoundExceptionusages — this exception is also thrown byArticleApi,ArticleFavoriteApi,CommentsApi, and several GraphQL resolvers. A missing article or comment will incorrectly return "Profile not found". Verify whether this is acceptable or if the message should be made generic (e.g."Resource not found") or derived from the exception.{"message": "..."}meets frontend expectations (noerrorswrapper, etc.)GET /api/profiles/null,POST /api/profiles/null/follow, andDELETE /api/profiles/null/followto confirm all three return the expected 404 body end-to-end.Notes