Skip to content

Feat/return 404 on null results#50

Merged
guto-alves merged 7 commits intomainfrom
feat/return-404-on-null-results
Jan 20, 2026
Merged

Feat/return 404 on null results#50
guto-alves merged 7 commits intomainfrom
feat/return-404-on-null-results

Conversation

@guto-alves
Copy link
Owner

This pull request refactors the ApiRestController to improve error handling and message clarity, and significantly enhances the test coverage for the API endpoints. The controller now provides more informative error messages for invalid lottery IDs and missing results, and the tests have been rewritten to cover a wide range of scenarios, including edge cases and error responses.

Error handling and messaging improvements

  • Standardized and clarified error messages for invalid lottery IDs and missing results in ApiRestController, using new constants for message formatting and replacing the previous message construction logic. [1] [2]
  • Added checks for null results in getResultById and getLatestResult endpoints, throwing a ResourceNotFoundException with a specific message if no result is found.

Testing enhancements

  • Refactored ApiRestControllerTests to use @WebMvcTest and @MockBean for isolated controller testing, and added a comprehensive set of tests covering all main endpoints, including success, not found, empty results, invalid lottery, and error response structure.
  • Added helper methods and test data setup to streamline the creation of test cases and verify response content and structure for various lottery types and scenarios.

Edge case coverage

  • Extended tests to cover edge cases such as zero and negative contest numbers, multiple results, accumulated prizes, and special contests, ensuring robust API behavior.

@gemini-code-assist
Copy link

Summary of Changes

Hello @guto-alves, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the API's error handling mechanisms by ensuring that requests for non-existent lottery data or invalid lottery IDs consistently return a 404 Not Found status with clear, standardized messages. Concurrently, the associated test suite has been significantly upgraded to provide comprehensive coverage for these new error behaviors and existing functionalities, ensuring the API's robustness and predictable responses.

Highlights

  • Improved Error Handling: The ApiRestController now explicitly returns a 404 (ResourceNotFoundException) for invalid lottery IDs, specific results not found, and when no latest result is available, enhancing API clarity and consistency.
  • Standardized Error Messages: New static final String constants have been introduced for error messages, ensuring consistent and informative responses across different error scenarios.
  • Enhanced Test Coverage: The ApiRestControllerTests have been extensively refactored and expanded to cover a wide array of scenarios, including successful data retrieval, various 'not found' conditions, empty results, and edge cases like zero or negative contest numbers.
  • Isolated Controller Testing: Tests now leverage @WebMvcTest and @MockBean for ResultadoService, promoting more isolated, faster, and reliable testing of the API controller layer.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step forward in improving the API's error handling and test coverage. Refactoring the ApiRestController to return a 404 status for null results is a significant improvement, and the move to @WebMvcTest with comprehensive test cases is excellent. I've identified a few areas for improvement, including a logic bug in how getLatestResult handles missing results, which is currently masked by a test with a mismatched mock. Additionally, there's an opportunity to refactor some duplicated code for better maintainability.

Comment on lines 57 to 59
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(String.format(invalidLotteryMessageFormat, loteria));
throw new ResourceNotFoundException(INVALID_LOTTERY_MESSAGE.formatted(loteria, lotteries));
}

Choose a reason for hiding this comment

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

medium

This lottery validation logic is duplicated across getResultsByLottery, getResultById, and getLatestResult. To improve maintainability and reduce code duplication, consider extracting this check into a private helper method, for example:

private void validateLottery(String loteria) {
    if (!lotteries.contains(loteria)) {
        throw new ResourceNotFoundException(INVALID_LOTTERY_MESSAGE.formatted(loteria, lotteries));
    }
}

You could then call validateLottery(loteria); at the beginning of each of these endpoint methods.

Comment on lines +220 to +221
List<String> lotteries = Arrays.asList("maismilionaria", "megasena", "lotofacil", "quina",
"lotomania", "timemania", "duplasena", "federal", "diadesorte", "supersete");

Choose a reason for hiding this comment

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

medium

The list of lotteries is hardcoded here and also in testGetLatestResultAllLotteries. To improve maintainability and ensure the test stays in sync with the available lotteries, it's better to retrieve this list dynamically from the Loteria enum using Loteria.asList().

        List<String> lotteries = com.gutotech.loteriasapi.model.Loteria.asList();

@guto-alves guto-alves merged commit 8373197 into main Jan 20, 2026
3 checks passed
@guto-alves guto-alves deleted the feat/return-404-on-null-results branch January 20, 2026 00:00
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