Skip to content

Reaching GenAI usage limit Exception (internal)#195

Merged
ntgbaoo merged 5 commits into
devfrom
task/194-GeminiException
Mar 28, 2026
Merged

Reaching GenAI usage limit Exception (internal)#195
ntgbaoo merged 5 commits into
devfrom
task/194-GeminiException

Conversation

@ntgbaoo
Copy link
Copy Markdown
Member

@ntgbaoo ntgbaoo commented Mar 27, 2026

Summary / Description

Detect when we reach Gemini usage limit and return 503 instead of 500

Related Issues: # (issue number)

Type of Change

  • Bug fix
  • New feature
  • Breaking Change
  • Refactoring
  • Documentation update

Test Evidence

Describe how this PR has been tested.

  • Unit tests
  • Integration tests

Questions / Discussion Points

List any areas where you’d like reviewer input or have open questions.

@ntgbaoo ntgbaoo self-assigned this Mar 27, 2026
@ntgbaoo ntgbaoo added documentation Improvements or additions to documentation enhancement New feature or request dev-task A piece of task under a user story labels Mar 27, 2026
@ntgbaoo ntgbaoo added this to the Sprint 4 milestone Mar 27, 2026
@ntgbaoo ntgbaoo marked this pull request as ready for review March 27, 2026 22:17
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ntgbaoo ntgbaoo requested review from ACM02, EricHodgson and gloox March 27, 2026 22:34
Comment on lines +408 to +420
@ExceptionHandler(GenAIServiceException.class)
public ResponseEntity<Map<String, Object>> handleGenAIServiceException(GenAIServiceException ex) {

String errorMessage = "ERROR: GenAI Service failed: " + ex.getMessage();
log.error(errorMessage);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(
Map.of(
"error",
SystemErrorCode.INTERNAL_ERROR,
"message",
"GenAI Service failed. Please try again later."));
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I realized I missed handling this exception so I added it here

gloox
gloox previously approved these changes Mar 27, 2026
Comment thread src/main/java/com/backend/coapp/handler/GlobalExceptionHandler.java Outdated
geminiClient.models.generateContent(this.model, prompt, null);
return response.text();
} catch (Exception e) {
if (e.getMessage().contains("429")) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

think we could do something like this? not sure if that works with this Exception object but im not a huge fan of checking the message like this.

if (e.getStatusCode() == HttpStatus.TOO_MANY_REQUESTS) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point. Tks. But getStatusCode is not defined in Exception. But your idea is right, there is a better way to do this:

catch (ApiException e) {
    if (e.code() == HttpStatus.TOO_MANY_REQUESTS.value() || (e.code() >= 500 && e.code() < 600)) {
      // This includes 503 - SERVICE UNAVAILABLE and 500 - INTERNAL ERROR
      throw new GenAIOutOfServiceException(e.getMessage());
    }
    throw new GenAIServiceException(e.getMessage());
}

@sonarqubecloud
Copy link
Copy Markdown

@ntgbaoo ntgbaoo merged commit e9d29f7 into dev Mar 28, 2026
9 checks passed
@ntgbaoo ntgbaoo linked an issue Apr 1, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dev-task A piece of task under a user story documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Detect reaching usage limit for Gemini

3 participants