Skip to content

Commit

Permalink
feat: Add support for Help and LocalizedMessage status details
Browse files Browse the repository at this point in the history
Fixes b/336945856
  • Loading branch information
jskeet committed Apr 25, 2024
1 parent 0501add commit 159dcb9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
18 changes: 17 additions & 1 deletion Google.Api.Gax.Grpc.Tests/RpcExceptionExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using GrpcStatus = Grpc.Core.Status;
using Google.Protobuf.WellKnownTypes;
using Google.Protobuf;
using static Google.Rpc.Help.Types;

namespace Google.Api.Gax.Grpc.Tests
{
Expand All @@ -28,6 +29,8 @@ public void NullArgument_AllMethodsThrow()
Assert.Throws<ArgumentNullException>(() => ex.GetRpcStatus());
Assert.Throws<ArgumentNullException>(() => ex.GetBadRequest());
Assert.Throws<ArgumentNullException>(() => ex.GetErrorInfo());
Assert.Throws<ArgumentNullException>(() => ex.GetHelp());
Assert.Throws<ArgumentNullException>(() => ex.GetLocalizedMessage());
Assert.Throws<ArgumentNullException>(() => ex.GetStatusDetail<BadRequest>());
}

Expand Down Expand Up @@ -72,6 +75,8 @@ public void ValidTrailer_GetRpcStatus()
// We don't have any details
Assert.Null(ex.GetBadRequest());
Assert.Null(ex.GetErrorInfo());
Assert.Null(ex.GetHelp());
Assert.Null(ex.GetLocalizedMessage());
Assert.Null(ex.GetStatusDetail<DebugInfo>());
}

Expand All @@ -90,6 +95,11 @@ public void ValidTrailer_ArbitraryMessages()
{
FieldViolations = { new BadRequest.Types.FieldViolation { Description = "Negative", Field = "speed" } }
};
var help = new Help
{
Links = { new Link { Description = "Google it", Url = "https://google.com" } }
};
var localizedMessage = new LocalizedMessage { Locale = "en-US", Message = "Oops" };
var status = new Status
{
Code = 123,
Expand All @@ -98,7 +108,9 @@ public void ValidTrailer_ArbitraryMessages()
{
Any.Pack(debugInfo),
Any.Pack(requestInfo),
Any.Pack(badRequest)
Any.Pack(badRequest),
Any.Pack(help),
Any.Pack(localizedMessage)
}
};

Expand All @@ -114,6 +126,8 @@ public void ValidTrailer_ArbitraryMessages()
Assert.Equal(debugInfo, ex.GetStatusDetail<DebugInfo>());
Assert.Equal(requestInfo, ex.GetStatusDetail<RequestInfo>());
Assert.Equal(badRequest, ex.GetStatusDetail<BadRequest>());
Assert.Equal(help, ex.GetStatusDetail<Help>());
Assert.Equal(localizedMessage, ex.GetStatusDetail<LocalizedMessage>());
}

[Fact]
Expand Down Expand Up @@ -161,6 +175,8 @@ private void AssertAllMethodsReturnNull(RpcException ex)
Assert.Null(ex.GetRpcStatus());
Assert.Null(ex.GetBadRequest());
Assert.Null(ex.GetErrorInfo());
Assert.Null(ex.GetHelp());
Assert.Null(ex.GetLocalizedMessage());
Assert.Null(ex.GetStatusDetail<DebugInfo>());
}
}
Expand Down
18 changes: 18 additions & 0 deletions Google.Api.Gax.Grpc/RpcExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ public static Rpc.Status GetRpcStatus(this RpcException ex) =>
/// if there is no such information.</returns>
public static ErrorInfo GetErrorInfo(this RpcException ex) => GetStatusDetail<ErrorInfo>(ex);

/// <summary>
/// Retrieves the <see cref="Help"/> message containing extended error information
/// from the trailers in an <see cref="RpcException"/>, if present.
/// </summary>
/// <param name="ex">The RPC exception to retrieve details from. Must not be null.</param>
/// <returns>The <see cref="Help"/> message specified in the exception, or null
/// if there is no such information.</returns>
public static Help GetHelp(this RpcException ex) => GetStatusDetail<Help>(ex);

/// <summary>
/// Retrieves the <see cref="LocalizedMessage"/> message containing extended error information
/// from the trailers in an <see cref="RpcException"/>, if present.
/// </summary>
/// <param name="ex">The RPC exception to retrieve details from. Must not be null.</param>
/// <returns>The <see cref="Help"/> message specified in the exception, or null
/// if there is no such information.</returns>
public static LocalizedMessage GetLocalizedMessage(this RpcException ex) => GetStatusDetail<LocalizedMessage>(ex);

/// <summary>
/// Retrieves the error details of type <typeparamref name="T"/> from the <see cref="Status"/>
/// message associated with an <see cref="RpcException"/>, if any.
Expand Down

0 comments on commit 159dcb9

Please sign in to comment.