Skip to content

Add exception handler and tests for duplicate budget constraint#16

Merged
LofoWalker merged 2 commits intofeat/epic-3-budget-importfrom
copilot/sub-pr-15
Feb 12, 2026
Merged

Add exception handler and tests for duplicate budget constraint#16
LofoWalker merged 2 commits intofeat/epic-3-budget-importfrom
copilot/sub-pr-15

Conversation

Copy link

Copilot AI commented Feb 12, 2026

The API lacked proper HTTP error handling for duplicate budget attempts, falling back to generic 422 errors instead of returning semantic 409 Conflict responses.

Changes

  • Exception mapping: Added BudgetAlreadyExistsException case to GlobalExceptionMapper returning 409 with BUDGET_ALREADY_EXISTS error code
  • Unit test: Added shouldThrowExceptionWhenBudgetAlreadyExists verifying domain exception is thrown when budget exists for current month
  • Integration test: Added shouldRejectDuplicateBudgetForSameMonth verifying full API flow returns 409 on duplicate attempts

API Response

POST /api/companies/{id}/budget
// Second call in same month returns:
{
  "error": {
    "code": "BUDGET_ALREADY_EXISTS",
    "message": "A budget already exists for this company for the current month",
    "traceId": "..."
  }
}

The duplicate detection logic (findByCompanyIdAndEffectiveFrom check) was already present from commit 848eb2d.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Added BudgetAlreadyExistsException handler to GlobalExceptionMapper (returns 409)
- Added unit test for duplicate budget scenario in SetCompanyBudgetUseCaseImplTest
- Added integration test for duplicate budget via API in BudgetResourceTest
- All tests passing successfully

Co-authored-by: LofoWalker <49187988+LofoWalker@users.noreply.github.com>
Copy link
Author

Copilot AI commented Feb 12, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.24.0/x64/codeql/xml/tools/xml-extractor.jar --fileList=/home/REDACTED/work/UpKeep/.codeql-scratch/dbs/java/working/files-to-index17825483527706406351.list --sourceArchiveDir=/home/REDACTED/work/UpKeep/.codeql-scratch/dbs/java/src --outputDir=/home/REDACTED/work/UpKeep/.codeql-scratch/dbs/java/trap/java --global rgo/bin/bash pull.rebase (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title [WIP] Update stack PR to address feedback on budget import Add exception handler and tests for duplicate budget constraint Feb 12, 2026
Copilot AI requested a review from LofoWalker February 12, 2026 15:50
@LofoWalker LofoWalker marked this pull request as ready for review February 12, 2026 15:52
Copilot AI review requested due to automatic review settings February 12, 2026 15:52
@LofoWalker LofoWalker merged commit d9101cc into feat/epic-3-budget-import Feb 12, 2026
4 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds explicit REST-layer handling for duplicate monthly company budget creation so the API returns a semantic 409 Conflict (with a specific error code) instead of the generic domain fallback 422.

Changes:

  • Added BudgetAlreadyExistsException handling to GlobalExceptionMapper, returning HTTP 409 with BUDGET_ALREADY_EXISTS.
  • Added a use-case unit test asserting BudgetAlreadyExistsException is thrown and no persistence/audit occurs.
  • Added an integration test asserting the second budget POST in the same month is rejected with 409 and the expected error code.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
apps/api/src/main/java/com/upkeep/infrastructure/adapter/in/rest/common/exception/GlobalExceptionMapper.java Maps BudgetAlreadyExistsException to HTTP 409 and emits BUDGET_ALREADY_EXISTS error code.
apps/api/src/test/java/com/upkeep/application/usecase/SetCompanyBudgetUseCaseImplTest.java Unit test coverage for duplicate budget detection at the use-case level.
apps/api/src/test/java/com/upkeep/infrastructure/adapter/in/rest/budget/BudgetResourceTest.java Integration test coverage ensuring the API returns 409 on duplicate monthly budget creation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@LofoWalker LofoWalker deleted the copilot/sub-pr-15 branch February 12, 2026 15:59
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.

3 participants