Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sdk/core/Azure.Core/src/RequestFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,14 @@ internal static bool TryExtractErrorContent(Response response, out ResponseError
return false;
}
// Try the ErrorResponse format and fallback to the ResponseError format.

#if NET6_0_OR_GREATER
error = System.Text.Json.JsonSerializer.Deserialize<ErrorResponse>(content, ResponseErrorSourceGenerationContext.Default.ErrorResponse)?.Error;
error ??= System.Text.Json.JsonSerializer.Deserialize<ResponseError>(content, ResponseErrorSourceGenerationContext.Default.ResponseError);
#else
error = System.Text.Json.JsonSerializer.Deserialize<ErrorResponse>(content)?.Error;
error ??= System.Text.Json.JsonSerializer.Deserialize<ResponseError>(content);
#endif
}
catch (Exception)
{
Expand All @@ -271,7 +277,8 @@ internal static bool TryExtractErrorContent(Response response, out ResponseError
return error != null;
}

private class ErrorResponse
// This class needs to be internal rather than private so that it can be used by the System.Text.Json source generator
internal class ErrorResponse
{
[System.Text.Json.Serialization.JsonPropertyName("error")]
public ResponseError? Error { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/Azure.Core/src/ResponseError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ internal ResponseError(string? code, string? message, string? target, JsonElemen
/// </summary>
internal IReadOnlyList<ResponseError> Details { get; }

private class Converter : JsonConverter<ResponseError?>
// This class needs to be internal rather than private so that it can be used by the System.Text.Json source generator
internal class Converter : JsonConverter<ResponseError?>
{
public override ResponseError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand Down
27 changes: 27 additions & 0 deletions sdk/core/Azure.Core/src/ResponseErrorSourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core;

namespace Azure
{
#if NET6_0_OR_GREATER
[JsonSerializable(typeof(ResponseError))]
[JsonSerializable(typeof(RequestFailedException.ErrorResponse))]
[JsonSerializable(typeof(ResponseInnerError))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(JsonDocument))]
[JsonSerializable(typeof(JsonValueKind))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(List<ResponseError>))]
internal partial class ResponseErrorSourceGenerationContext : JsonSerializerContext
{
}
#endif
}