Skip to content

Comments

fix: Add ResourceNotFoundException handler for structured 404 responses [MDD-78]#370

Open
devin-ai-integration[bot] wants to merge 2 commits intomasterfrom
devin/1771876443-mdd78-resource-not-found-handler
Open

fix: Add ResourceNotFoundException handler for structured 404 responses [MDD-78]#370
devin-ai-integration[bot] wants to merge 2 commits intomasterfrom
devin/1771876443-mdd78-resource-not-found-handler

Conversation

@devin-ai-integration
Copy link

@devin-ai-integration devin-ai-integration bot commented Feb 23, 2026

fix: Add structured 404 responses for ResourceNotFoundException [MDD-78]

Summary

Adds explicit @ExceptionHandler for ResourceNotFoundException in the REST exception handler and parallel handling in the GraphQL exception handler, so that invalid username lookups (and all other resource-not-found scenarios) return a structured {"message": "Resource not found"} JSON body instead of a raw, empty 404.

Changes:

  • CustomizeExceptionHandler: New handler method returning HttpStatus.NOT_FOUND with a JSON body, following the existing InvalidAuthenticationException pattern.
  • GraphQLCustomizeExceptionHandler: New else if branch returning a TypedGraphQLError with ErrorType.NOT_FOUND, inserted before the default handler fallback.
  • ProfileApiTest: Three new test cases covering GET profile, POST follow, and DELETE unfollow with nonexistent usernames.

This fix applies application-wide to all endpoints that throw ResourceNotFoundException (ProfileApi, ArticleApi, CommentsApi, ArticleFavoriteApi, etc.).

Updates since last revision

  • Removed unused import static org.mockito.ArgumentMatchers.any from ProfileApiTest.java.
  • Verified locally by running the app and curling all three profile endpoints with nonexistent usernames. Results confirm that the @ExceptionHandler takes precedence over the @ResponseStatus annotation on the exception class, and the structured JSON body is returned correctly:
    • GET /profiles/nonexistent_user404 {"message":"Resource not found"}
    • POST /profiles/nonexistent_user/follow (with auth) → 404 {"message":"Resource not found"}
    • DELETE /profiles/nonexistent_user/follow (with auth) → 404 {"message":"Resource not found"}

Review & Testing Checklist for Human

  • No GraphQL test coverage: The GraphQLCustomizeExceptionHandler change has no corresponding test. Consider whether a GraphQL-layer test should be added for the NOT_FOUND path to guard against regressions.
  • @ResponseStatus vs @ExceptionHandler precedence: ResourceNotFoundException has @ResponseStatus(HttpStatus.NOT_FOUND) on the class. Local testing confirmed the @ExceptionHandler wins and the structured body is returned, but verify this holds in your deployment environment as well since Spring Boot version or configuration differences could affect precedence.
  • Manual test plan: Start the app (./gradlew bootRun), register a user, then:
    # GET nonexistent profile (no auth needed)
    curl -s http://localhost:8080/profiles/nonexistent_user
    # POST follow nonexistent user (auth required)
    curl -s -X POST http://localhost:8080/profiles/nonexistent_user/follow -H "Authorization: Token <token>"
    # DELETE unfollow nonexistent user (auth required)
    curl -s -X DELETE http://localhost:8080/profiles/nonexistent_user/follow -H "Authorization: Token <token>"
    Confirm all return HTTP 404 with body {"message": "Resource not found"}.

Notes

  • The error message is intentionally generic ("Resource not found") to stay consistent across all resource types that use this exception.
  • All CI checks (build, Snyk security/license) pass.
  • Requested by: @milind-cognition
  • Link to Devin run

Add @ExceptionHandler for ResourceNotFoundException in CustomizeExceptionHandler
to return structured JSON {"message": "Resource not found"} instead of raw 404.

Add parallel handling in GraphQLCustomizeExceptionHandler with NOT_FOUND error type.

Add test cases for profile not found, follow nonexistent user, and unfollow
nonexistent user scenarios.

Addresses MDD-78.

Co-Authored-By: unknown <>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Co-Authored-By: unknown <>
Copy link
Author

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 5 additional findings.

Open in Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant