Skip to content

Commit

Permalink
add and use constants, more docs tidy up.
Browse files Browse the repository at this point in the history
Signed-off-by: jev jacob@jev.org.uk
  • Loading branch information
jev-e committed Dec 26, 2024
1 parent 46a5dc3 commit 8ebac53
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
4 changes: 1 addition & 3 deletions src/Dapr.Common/Exceptions/DaprExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ namespace Dapr.Common.Exceptions
/// </summary>
public static class DaprExceptionExtensions
{
private static string GrpcDetails = "grpc-status-details-bin";

/// <summary>
/// Attempt to retrieve <see cref="DaprExtendedErrorInfo"/> from <see cref="DaprException"/>.
/// </summary>
Expand All @@ -37,7 +35,7 @@ public static bool TryGetExtendedErrorInfo(this DaprException exception, [NotNul
return false;
}

var metadata = rpcException.Trailers.Get(GrpcDetails);
var metadata = rpcException.Trailers.Get(DaprExtendedErrorConstants.GrpcDetails);

if (metadata is null)
{
Expand Down
9 changes: 9 additions & 0 deletions src/Dapr.Common/Exceptions/DaprExtendedErrorConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Dapr.Common.Exceptions
{
internal class DaprExtendedErrorConstants
{
public const string DaprErrorDetailTypeUrl = "type.googleapis.com/";

public const string GrpcDetails = "grpc-status-details-bin";
}
}
4 changes: 2 additions & 2 deletions src/Dapr.Common/Exceptions/DaprExtendedErrorDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract record DaprExtendedErrorDetail(DaprExtendedErrorType ErrorType);
/// Detail when the type url is unrecognized.
/// </summary>
/// <param name="TypeUrl">The unrecognized type url.</param>
public sealed record DaprUnrecognizedDetail(string TypeUrl) : DaprExtendedErrorDetail(DaprExtendedErrorType.Unrecognized);
public sealed record DaprUnknownDetail(string TypeUrl) : DaprExtendedErrorDetail(DaprExtendedErrorType.Unknown);

/// <summary>
/// Detail proving debugging information.
Expand Down Expand Up @@ -76,7 +76,7 @@ public sealed record DaprRetryInfoDetail() : DaprExtendedErrorDetail(DaprExtende
public sealed record DaprQuotaFailureViolation(string Subject, string Description);

/// <summary>
/// Detail relating to a quota failure e.g reaching an API limit.
/// Detail relating to a quota failure e.g reaching API limit.
/// </summary>
public sealed record DaprQuotaFailureDetail() : DaprExtendedErrorDetail(DaprExtendedErrorType.QuotaFailure)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Dapr.Common/Exceptions/DaprExtendedErrorType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ namespace Dapr.Common.Exceptions
/// Extended error detail types.
/// This is based on the Richer Error Model (see <see href="https://google.aip.dev/193#error_model"/> and
/// <see href="https://github.com/googleapis/googleapis/blob/master/google/rpc/error_details.proto"/>)
/// and is implemented by the Dapr runtime (see <see href="https://github.com/dapr/dapr/blob/master/pkg/api/errors/README.md)."/>).
/// and is implemented by the Dapr runtime (see <see href="https://github.com/dapr/dapr/blob/master/pkg/api/errors/README.md"/>).
/// </summary>
public enum DaprExtendedErrorType
{
/// <summary>
/// Unrecognized extended error type.
/// Implemented by <see cref="DaprUnrecognizedDetail"/>.
/// Unknown extended error type.
/// Implemented by <see cref="DaprUnknownDetail"/>.
/// </summary>
Unrecognized,
Unknown,

/// <summary>
/// Retry info detail type.
Expand Down
4 changes: 2 additions & 2 deletions src/Dapr.Common/Exceptions/ExtendedErrorDetailFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Dapr.Common.Exceptions
/// </summary>
internal static class ExtendedErrorDetailFactory
{
private const string DaprErrorTypeUrl = "type.googleapis.com/";
private const string DaprErrorTypeUrl = DaprExtendedErrorConstants.DaprErrorDetailTypeUrl;

private static Dictionary<string, Func<ByteString, DaprExtendedErrorDetail>> extendedErrorTypeMapping =
new()
Expand All @@ -35,7 +35,7 @@ internal static DaprExtendedErrorDetail CreateErrorDetail(Any metadata)
{
if (!extendedErrorTypeMapping.TryGetValue(metadata.TypeUrl, out var create))
{
return new DaprUnrecognizedDetail(metadata.TypeUrl);
return new DaprUnknownDetail(metadata.TypeUrl);
}

return create.Invoke(metadata.Value);
Expand Down
26 changes: 12 additions & 14 deletions test/Dapr.Common.Test/DaprExtendedErrorInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public class DaprExtendedErrorInfoTest
private static int statusCode = 1;
private static string statusMessage = "Status Message";

private static string GrpcDetails = "grpc-status-details-bin";

[Fact]
public void DaprExendedErrorInfo_ThrowsRpcDaprException_ExtendedErrorInfoReturnsTrueAndNotNull()
{
Expand All @@ -32,7 +30,7 @@ public void DaprExendedErrorInfo_ThrowsRpcDaprException_ExtendedErrorInfoReturns

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "BadRequest"), trailers: trailers);
Expand Down Expand Up @@ -119,7 +117,7 @@ public void DaprExtendedErrorInfo_ThrowsBadRequestRpcException_ShouldGetSingleDa

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "BadRequest"), trailers: trailers);
Expand Down Expand Up @@ -171,7 +169,7 @@ public void DaprExtendedErrorInfo_ThrowsLocalizedMessageRpcException_ShouldGetSi

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "BadRequest"), trailers: trailers);
Expand Down Expand Up @@ -220,7 +218,7 @@ public void DaprExtendedErrorInfo_ThrowRetryInfoRpcException_ShouldGetSingleDapr

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.FailedPrecondition, "RetryInfo"), trailers: trailers);
Expand Down Expand Up @@ -267,7 +265,7 @@ public void DaprExtendedErrorInfo_ThrowsDebugInfoRpcException_ShouldGetSingleDap

Grpc.Core.Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(Grpc.Core.StatusCode.FailedPrecondition, "DebugInfo"), trailers: trailers);
Expand Down Expand Up @@ -323,7 +321,7 @@ public void DaprExtendedErrorInfo_ThrowsPreconditionFailureRpcException_ShouldGe

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(Grpc.Core.StatusCode.FailedPrecondition, "PrecondtionFailure"), trailers: trailers);
Expand Down Expand Up @@ -379,7 +377,7 @@ public void DaprExtendedErrorInfo_ThrowsHelpRpcException_ShouldGetSingleDaprHelp

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "Help"), trailers: trailers);
Expand Down Expand Up @@ -438,7 +436,7 @@ public void DaprExtendedErrorInfo_ThrowsResourceInfoRpcException_ShouldGetSingle

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "ResourceInfo"), trailers: trailers);
Expand Down Expand Up @@ -494,7 +492,7 @@ public void DaprExtendedErrorInfo_ThrowsQuotaFailureRpcException_ShouldGetSingle

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "QuotaFailure"), trailers: trailers);
Expand Down Expand Up @@ -556,7 +554,7 @@ public void DaprExtendedErrorInfo_ThrowsErrorInfoRpcException_ShouldGetSingleDap

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "ErrorInfo"), trailers: trailers);
Expand Down Expand Up @@ -612,7 +610,7 @@ public void DaprExtendedErrorInfo_ThrowsRequestInfoRpcException_ShouldGetSingleD

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "RequestInfo"), trailers: trailers);
Expand Down Expand Up @@ -665,7 +663,7 @@ public void DaprExtendedErrorInfo_ThrowsRequestInfoRpcException_ShouldGetMultipl

Metadata trailers = new()
{
{ GrpcDetails, metadataEntry.ToByteArray() }
{ DaprExtendedErrorConstants.GrpcDetails, metadataEntry.ToByteArray() }
};

var rpcEx = new RpcException(status: new Grpc.Core.Status(StatusCode.Aborted, "RequestInfo"), trailers: trailers);
Expand Down

0 comments on commit 8ebac53

Please sign in to comment.