Skip to content

Populate ExecutionResult.Exception for non-MSAL failures (Bug 3696306) - #6139

Merged
Neha Bhargava (neha-bhargava) merged 6 commits into
mainfrom
nebharg/otel-enricher-nonmsal-exception
Jul 22, 2026
Merged

Neha Bhargava (neha-bhargava) merged 6 commits into
mainfrom
nebharg/otel-enricher-nonmsal-exception

Conversation

@neha-bhargava

Copy link
Copy Markdown
Contributor

Bug 3696306

WithOtelTagsEnricher received ExecutionResult.Exception == null when a token acquisition failed with a non-MsalException (e.g. an HttpRequestException from a federated-credential/client-assertion callback).

RequestBase's generic catch (Exception) passed no exception to LogFailureTelemetryToOtel, so the enricher's ExecutionResult.Exception was null β€” unlike the catch (MsalException) path (#6096), which passes a populated exception.

Fix

The non-MSAL catch now builds a telemetry-only MsalException wrapper and hands it to the enricher, mirroring the MsalException path:

  • ErrorCode = originating exception type (FullName)
  • Message = original message
  • InnerException = original exception (full fidelity)
  • AuthenticationResultMetadata = failure metadata (durations, region, cache reason)

The original exception is still re-thrown unchanged β€” this wrapper is never surfaced to callers, so caller-observable behavior (the exception type they catch) is unaffected.

Tests

Added WithOtelTagsEnricher_NonMsalFailure_ReceivesWrappedExceptionWithMetadataAsync: a throwing client-assertion callback yields a raw HttpRequestException; asserts the enricher observes the populated wrapper (type/inner/message/metadata) and that the caller still receives the original HttpRequestException. New test + all existing enricher tests pass.

Related

CHANGELOG intentionally not updated (done post-release).

The OTel tags enricher received ExecutionResult.Exception = null when a
token acquisition failed with a non-MsalException, because RequestBase's
generic catch passed no exception to LogFailureTelemetryToOtel. It now
hands the enricher a telemetry-only MsalException wrapper (originating
type as ErrorCode, original message, original exception as InnerException,
plus failure metadata), mirroring the MsalException path. The original
exception is still re-thrown unchanged, so caller-observable behavior is
unaffected.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
Copilot AI review requested due to automatic review settings July 21, 2026 21:04
@neha-bhargava
Neha Bhargava (neha-bhargava) requested a review from a team as a code owner July 21, 2026 21:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR ensures OpenTelemetry tag enrichers receive a populated ExecutionResult.Exception even when token acquisition fails with a non-MsalException (e.g., exceptions thrown from client-assertion/federated-credential callbacks). It does this by synthesizing a telemetry-only MsalException wrapper (carrying failure metadata) while rethrowing the original exception unchanged to preserve caller-observable behavior.

Changes:

  • Update RequestBase.RunAsync generic catch (Exception) path to pass a telemetry-only MsalException wrapper into LogFailureTelemetryToOtel, mirroring the catch (MsalException) behavior.
  • Add a unit test verifying (1) the caller still receives the original non-MSAL exception and (2) the OTel enricher observes a wrapped MsalException with metadata.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/client/Microsoft.Identity.Client/Internal/Requests/RequestBase.cs Builds and passes a telemetry-only MsalException wrapper (with failure metadata) to the OTel enricher for non-MSAL failures.
tests/Microsoft.Identity.Test.Unit/TelemetryTests/OTelInstrumentationTests.cs Adds a regression test covering the non-MSAL exception path and validating enricher + caller behaviors.

…g 3696306)

Address PR review: the non-MSAL enricher wrapper used ex.GetType().FullName
and ex.Message directly, but the MsalException ctor rejects a null/whitespace
errorCode or errorMessage. An original exception with an empty message (or a
null Type.FullName for some generic/array types) would therefore raise an
ArgumentNullException that replaced the original exception, breaking the
rethrow-unchanged guarantee. Fall back to the type name in both cases and add
a regression test for the empty-message path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
Copilot AI review requested due to automatic review settings July 22, 2026 17:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

Copilot AI review requested due to automatic review settings July 22, 2026 18:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

…on.Data (3696194) (#6140)

Expose AuthenticationResultMetadata on non-MSAL failures via Exception.Data (Bug 3696194)

When a token acquisition fails with a non-MsalException, MSAL now stashes the
failure AuthenticationResultMetadata (durations, cache-refresh reason, token
endpoint, region) on the original exception's Data bag under the new public
MsalException.AuthenticationResultMetadataKey. Downstream header-creation
providers that catch the raw exception can surface token-acquisition
diagnostics without MSAL converting or wrapping the thrown exception. The
original exception is re-thrown unchanged; the write is guarded so it never
masks the caller's exception.


Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 22, 2026 18:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

…tstandard (3696194)

The Exception.Data write added for 3696194 throws ArgumentException 'not serializable' on .NET Framework, whose ListDictionaryInternal rejects non-serializable values, replacing the caller's original exception (17 net48 test failures). .NET Core removed that check. Mark AuthenticationResultMetadata and its RegionDetails member [Serializable] only under NETFRAMEWORK || NETSTANDARD; the whole graph is serializable so the write succeeds on every target and metadata is delivered uniformly. No public-API or test changes needed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: d296b5aa-33db-4934-a697-cdf1c1999e09
Copilot AI review requested due to automatic review settings July 22, 2026 20:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.

…-nonmsal-exception

# Conflicts:
#	src/client/Microsoft.Identity.Client/PublicApi/net462/PublicAPI.Unshipped.txt
#	src/client/Microsoft.Identity.Client/PublicApi/net472/PublicAPI.Unshipped.txt
#	src/client/Microsoft.Identity.Client/PublicApi/net8.0-android/PublicAPI.Unshipped.txt
#	src/client/Microsoft.Identity.Client/PublicApi/net8.0-ios/PublicAPI.Unshipped.txt
#	src/client/Microsoft.Identity.Client/PublicApi/net8.0/PublicAPI.Unshipped.txt
#	src/client/Microsoft.Identity.Client/PublicApi/netstandard2.0/PublicAPI.Unshipped.txt
Copilot AI review requested due to automatic review settings July 22, 2026 21:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@neha-bhargava
Neha Bhargava (neha-bhargava) merged commit 1159aa4 into main Jul 22, 2026
17 checks passed
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.

4 participants