Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
Signed-off-by: jev <[email protected]>
  • Loading branch information
jev-e committed Dec 27, 2024
1 parent 581aedf commit 6e81dae
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ catch (DaprException daprEx)
case ExtendedErrorType.ErrorInfo:
Console.WriteLine(detail.Reason);
Console.WriteLine(detail.Domain);
default:
Console.WriteLine(detail.TypeUrl);
}
}
}
Expand Down Expand Up @@ -83,7 +85,7 @@ Debugging information offered by the server. Contains `StackEntries` (a collecti

## QuotaFailure

Information relating to some quota that may have been reach, such as a daily usage limit on an API. It has one property `Violations`,
Information relating to some quota that may have been reached, such as a daily usage limit on an API. It has one property `Violations`,
a collection of `DaprQuotaFailureViolation`, which each contain a `Subject` (the subject of the request) and `Description` (further information regarding the failure).

## PreconditionFailure
Expand Down
31 changes: 28 additions & 3 deletions src/Dapr.Common/Exceptions/DaprExtendedErrorConstants.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
namespace Dapr.Common.Exceptions
// ------------------------------------------------------------------------
// Copyright 2024 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.Common.Exceptions
{
internal class DaprExtendedErrorConstants
/// <summary>
/// Definitions of expected types to be returned from the Dapr runtime.
/// </summary>
internal static class DaprExtendedErrorConstants
{
public const string ErrorDetailTypeUrl = "type.googleapis.com/";

public const string GrpcDetails = "grpc-status-details-bin";
public const string ErrorInfo = $"{ErrorDetailTypeUrl}Google.rpc.ErrorInfo";
public const string RetryInfo = $"{ErrorDetailTypeUrl}Google.rpc.RetryInfo";
public const string DebugInfo = $"{ErrorDetailTypeUrl}Google.rpc.DebugInfo";
public const string QuotaFailure = $"{ErrorDetailTypeUrl}Google.rpc.QuotaFailure";
public const string PreconditionFailure = $"{ErrorDetailTypeUrl}Google.rpc.PreconditionFailure";
public const string BadRequest = $"{ErrorDetailTypeUrl}Google.rpc.BadRequest";
public const string RequestInfo = $"{ErrorDetailTypeUrl}Google.rpc.RequestInfo";
public const string ResourceInfo = $"{ErrorDetailTypeUrl}Google.rpc.ResourceInfo";
public const string Help = $"{ErrorDetailTypeUrl}Google.rpc.Help";
public const string LocalizedMessage = $"{ErrorDetailTypeUrl}Google.rpc.LocalizedMessage";
}
}
10 changes: 5 additions & 5 deletions src/Dapr.Common/Exceptions/DaprExtendedErrorDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed record DaprUnknownDetail(string TypeUrl) : DaprExtendedErrorDetail
/// </summary>
/// <param name="StackEntries">Stack trace entries relating to error.</param>
/// <param name="Detail">Further related debugging information.</param>
public sealed record DaprDebugInfoDetail(string[] StackEntries, string Detail) : DaprExtendedErrorDetail(DaprExtendedErrorType.DebugInfo);
public sealed record DaprDebugInfoDetail(IReadOnlyCollection<string> StackEntries, string Detail) : DaprExtendedErrorDetail(DaprExtendedErrorType.DebugInfo);

/// <summary>
/// A precondtion violation.
Expand All @@ -47,7 +47,7 @@ public sealed record DaprPreconditionFailureDetail() : DaprExtendedErrorDetail(D
/// <summary>
/// Collection of <see cref="DaprBadRequestDetailFieldViolation"/>.
/// </summary>
public DaprPreconditionFailureViolation[] Violations { get; init; } = Array.Empty<DaprPreconditionFailureViolation>();
public IReadOnlyCollection<DaprPreconditionFailureViolation> Violations { get; init; } = Array.Empty<DaprPreconditionFailureViolation>();
}

/// <summary>
Expand Down Expand Up @@ -83,7 +83,7 @@ public sealed record DaprQuotaFailureDetail() : DaprExtendedErrorDetail(DaprExte
/// <summary>
/// Collection of <see cref="DaprQuotaFailureViolation"/>.
/// </summary>
public DaprQuotaFailureViolation[] Violations { get; init; } = Array.Empty<DaprQuotaFailureViolation>();
public IReadOnlyCollection<DaprQuotaFailureViolation> Violations { get; init; } = Array.Empty<DaprQuotaFailureViolation>();
}

/// <summary>
Expand All @@ -101,7 +101,7 @@ public sealed record DaprBadRequestDetail() : DaprExtendedErrorDetail(DaprExtend
/// <summary>
/// Collection of <see cref="DaprBadRequestDetailFieldViolation"/>.
/// </summary>
public DaprBadRequestDetailFieldViolation[] FieldViolations { get; init; } = Array.Empty<DaprBadRequestDetailFieldViolation>();
public IReadOnlyCollection<DaprBadRequestDetailFieldViolation> FieldViolations { get; init; } = Array.Empty<DaprBadRequestDetailFieldViolation>();
}

/// <summary>
Expand Down Expand Up @@ -134,7 +134,7 @@ public sealed record DaprHelpDetail() : DaprExtendedErrorDetail(DaprExtendedErro
/// <summary>
/// Collection of <see cref="DaprHelpDetailLink"/>.
/// </summary>
public DaprHelpDetailLink[] Links { get; init; } = Array.Empty<DaprHelpDetailLink>();
public IReadOnlyCollection<DaprHelpDetailLink> Links { get; init; } = Array.Empty<DaprHelpDetailLink>();
}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion src/Dapr.Common/Exceptions/DaprExtendedErrorInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
namespace Dapr.Common.Exceptions
// ------------------------------------------------------------------------
// Copyright 2024 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

namespace Dapr.Common.Exceptions
{
/// <summary>
/// Dapr implementation of the richer error model.
Expand Down
51 changes: 28 additions & 23 deletions src/Dapr.Common/Exceptions/ExtendedErrorDetailFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
using Google.Protobuf;
// ------------------------------------------------------------------------
// Copyright 2024 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
using Google.Rpc;

Expand All @@ -9,36 +22,28 @@ namespace Dapr.Common.Exceptions
/// </summary>
internal static class ExtendedErrorDetailFactory
{
private const string DaprErrorTypeUrl = DaprExtendedErrorConstants.ErrorDetailTypeUrl;

private static Dictionary<string, Func<ByteString, DaprExtendedErrorDetail>> extendedErrorTypeMapping =
new()
{
{ $"{DaprErrorTypeUrl}Google.rpc.ErrorInfo", ToDaprErrorInfoDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.RetryInfo", ToDaprRetryInfoDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.DebugInfo", ToDaprDebugInfoDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.QuotaFailure", ToDaprQuotaFailureDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.PreconditionFailure", ToDaprPreconditionFailureDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.BadRequest", ToDaprBadRequestDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.RequestInfo", ToDaprRequestInfoDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.ResourceInfo", ToDaprResourceInfoDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.Help", ToDaprHelpDetail },
{ $"{DaprErrorTypeUrl}Google.rpc.LocalizedMessage", ToDaprLocalizedMessageDetail },
};

/// <summary>
/// Create a new <see cref="DaprExtendedErrorDetail"/> from an instance of <see cref="Any"/>.
/// </summary>
/// <param name="message">The serialized detail message to create the error detail from.</param>
/// <returns>A new instance of <see cref="DaprExtendedErrorDetail"/></returns>
internal static DaprExtendedErrorDetail CreateErrorDetail(Any message)
{
if (!extendedErrorTypeMapping.TryGetValue(message.TypeUrl, out var create))
var data = message.Value;
return message.TypeUrl switch
{
return new DaprUnknownDetail(message.TypeUrl);
}

return create.Invoke(message.Value);
DaprExtendedErrorConstants.RetryInfo => ToDaprRetryInfoDetail(data),
DaprExtendedErrorConstants.ErrorInfo => ToDaprErrorInfoDetail(data),
DaprExtendedErrorConstants.DebugInfo => ToDaprDebugInfoDetail(data),
DaprExtendedErrorConstants.QuotaFailure => ToDaprQuotaFailureDetail(data),
DaprExtendedErrorConstants.PreconditionFailure => ToDaprPreconditionFailureDetail(data),
DaprExtendedErrorConstants.BadRequest => ToDaprBadRequestDetail(data),
DaprExtendedErrorConstants.RequestInfo => ToDaprRequestInfoDetail(data),
DaprExtendedErrorConstants.ResourceInfo => ToDaprResourceInfoDetail(data),
DaprExtendedErrorConstants.Help => ToDaprHelpDetail(data),
DaprExtendedErrorConstants.LocalizedMessage => ToDaprLocalizedMessageDetail(data),
_ => new DaprUnknownDetail(message.TypeUrl)
};
}

private static DaprRetryInfoDetail ToDaprRetryInfoDetail(ByteString data)
Expand Down

0 comments on commit 6e81dae

Please sign in to comment.