Skip to content

fix: Use KeyNotFoundException for not-found conditions in core Azure services#2703

Merged
tmeschter merged 1 commit into
microsoft:mainfrom
tmeschter:260521-FixStatusCodes
May 22, 2026
Merged

fix: Use KeyNotFoundException for not-found conditions in core Azure services#2703
tmeschter merged 1 commit into
microsoft:mainfrom
tmeschter:260521-FixStatusCodes

Conversation

@tmeschter
Copy link
Copy Markdown
Member

@tmeschter tmeschter commented May 21, 2026

Summary

Replace plain Exception and ArgumentException with KeyNotFoundException in SubscriptionService, TenantService, and ResourceGroupService so that not-found conditions return HTTP 404 instead of 500/400.

Changes

File Lines Before After
SubscriptionService.cs 79 Exception (→500) KeyNotFoundException (→404)
SubscriptionService.cs 97-98 ArgumentException (→400) KeyNotFoundException (→404)
SubscriptionService.cs 106-107 ArgumentException (→400) KeyNotFoundException (→404)
TenantService.cs 98 Exception (→500) KeyNotFoundException (→404)
TenantService.cs 111 Exception (→500) KeyNotFoundException (→404)
ResourceGroupService.cs 97 Exception (→500) KeyNotFoundException (→404)

Why KeyNotFoundException?

AuthenticatedCommand.GetStatusCode already maps KeyNotFoundExceptionHttpStatusCode.NotFound (404). This is the same pattern used by BaseAzureResourceService and AcrService for not-found conditions.

Validation

  • Build: Clean ✅
  • Tests: 1109 passed, 0 failed ✅

Fixes microsoft/mcp-pr#396
Fixes microsoft/mcp-pr#403
Fixes microsoft/mcp-pr#404

Invoking Livetests

Copilot submitted PRs are not trustworthy by default. Users with write access to the repo need to validate the contents of this PR before leaving a comment with the text /azp run mcp - pullrequest - live. This will trigger the necessary livetest workflows to complete required validation.

…services

Replace plain Exception and ArgumentException with KeyNotFoundException in
SubscriptionService, TenantService, and ResourceGroupService so that not-found
conditions return HTTP 404 instead of 500/400.

Fixes: microsoft/mcp-pr#396
Fixes: microsoft/mcp-pr#403
Fixes: microsoft/mcp-pr#404

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@tmeschter tmeschter requested a review from a team as a code owner May 21, 2026 19:41
Copilot AI review requested due to automatic review settings May 21, 2026 19:41
@tmeschter tmeschter requested a review from a team as a code owner May 21, 2026 19:41
Copy link
Copy Markdown
Contributor

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

Updates core Azure services to treat “not found” conditions as KeyNotFoundException so the command layer maps them to HTTP 404 (instead of 500/400), aligning behavior with existing patterns elsewhere in the repo.

Changes:

  • Switched not-found throws in SubscriptionService and TenantService to KeyNotFoundException.
  • Switched resource-group not-found throw in ResourceGroupService to KeyNotFoundException.
  • Added an Azure server changelog entry describing the HTTP status code fix.
Show a summary per file
File Description
servers/Azure.Mcp.Server/changelog-entries/1779392230582.yaml Adds changelog note for the not-found → 404 behavior change.
core/Azure.Mcp.Core/src/Services/Azure/Tenant/TenantService.cs Uses KeyNotFoundException for tenant not-found lookups so callers map to 404.
core/Azure.Mcp.Core/src/Services/Azure/Subscription/SubscriptionService.cs Uses KeyNotFoundException for subscription not-found lookups so callers map to 404.
core/Azure.Mcp.Core/src/Services/Azure/ResourceGroup/ResourceGroupService.cs Uses KeyNotFoundException when resource group is missing during generic resource enumeration.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment thread core/Azure.Mcp.Core/src/Services/Azure/Tenant/TenantService.cs
@github-project-automation github-project-automation Bot moved this from Untriaged to In Progress in Azure MCP Server May 22, 2026
@tmeschter tmeschter merged commit 63a7f51 into microsoft:main May 22, 2026
25 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Azure MCP Server May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants