Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at Grpc.StatusProto API #2205

Merged
merged 15 commits into from
Jan 4, 2024
Merged
14 changes: 14 additions & 0 deletions Grpc.DotNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.HealthCheck.Tests", "t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.Reflection.Tests", "test\Grpc.Reflection.Tests\Grpc.Reflection.Tests.csproj", "{857C5B4B-E2A8-4ACA-98FB-5E592E2224CC}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.StatusProto", "src\Grpc.StatusProto\Grpc.StatusProto.csproj", "{C01E4F44-9AB0-4478-A453-C88CCB49A4F1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Grpc.StatusProto.Tests", "test\Grpc.StatusProto.Tests\Grpc.StatusProto.Tests.csproj", "{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -302,6 +306,14 @@ Global
{857C5B4B-E2A8-4ACA-98FB-5E592E2224CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{857C5B4B-E2A8-4ACA-98FB-5E592E2224CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{857C5B4B-E2A8-4ACA-98FB-5E592E2224CC}.Release|Any CPU.Build.0 = Release|Any CPU
{C01E4F44-9AB0-4478-A453-C88CCB49A4F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C01E4F44-9AB0-4478-A453-C88CCB49A4F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C01E4F44-9AB0-4478-A453-C88CCB49A4F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C01E4F44-9AB0-4478-A453-C88CCB49A4F1}.Release|Any CPU.Build.0 = Release|Any CPU
{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -355,6 +367,8 @@ Global
{B4153E7F-5CF3-4DFB-A9D1-5E77A2FB2C48} = {8C62055F-8CD7-4859-9001-634D544DF2AE}
{25544326-C145-4D05-A4C3-AC7D59E17196} = {CECC4AE8-9C4E-4727-939B-517CC2E58D65}
{857C5B4B-E2A8-4ACA-98FB-5E592E2224CC} = {CECC4AE8-9C4E-4727-939B-517CC2E58D65}
{C01E4F44-9AB0-4478-A453-C88CCB49A4F1} = {8C62055F-8CD7-4859-9001-634D544DF2AE}
{E49FA5BF-4D67-4C95-9543-8E9FCEAF3609} = {CECC4AE8-9C4E-4727-939B-517CC2E58D65}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CD5C2B19-49B4-480A-990C-36D98A719B07}
Expand Down
1 change: 1 addition & 0 deletions build/dependencies.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<PropertyGroup>
<BenchmarkDotNetPackageVersion>0.13.1</BenchmarkDotNetPackageVersion>
<GoogleApiCommonProtos>2.10.0</GoogleApiCommonProtos>
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
<GoogleApisAuthPackageVersion>1.46.0</GoogleApisAuthPackageVersion>
<GoogleProtobufPackageVersion>3.23.1</GoogleProtobufPackageVersion>
<GrpcDotNetPackageVersion>2.52.0</GrpcDotNetPackageVersion> <!-- Used by example projects -->
Expand Down
23 changes: 23 additions & 0 deletions src/Grpc.StatusProto/Grpc.StatusProto.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>gRPC C# API for error handling using status.proto</Description>
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
<PackageTags>gRPC RPC HTTP/2</PackageTags>

<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Api.CommonProtos" Version="$(GoogleApiCommonProtos)" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />

<ProjectReference Include="..\Grpc.Core.Api\Grpc.Core.Api.csproj" />
</ItemGroup>

</Project>
75 changes: 75 additions & 0 deletions src/Grpc.StatusProto/MetadataExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 gRPC 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 Grpc.Core;

namespace Grpc.StatusProto;

/// <summary>
/// Extension methods for the Grpc.Core.Metadata
/// </summary>
public static class MetadataExtensions
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Name of key in the metadata for the binary encoding of
/// <see cref="Google.Rpc.Status"/>
/// </summary>
public const string StatusDetailsTrailerName = "grpc-status-details-bin";

/// <summary>
/// Get the <see cref="Google.Rpc.Status"/> from the metadata.
/// If the metadata received contains duplicate status details then the last one found
/// is the one that is returned.
/// </summary>
/// <param name="metadata"></param>
/// <returns>
/// The found <see cref="Google.Rpc.Status"/> or null if it was
/// not present or could the data could not be parsed.
/// </returns>
public static Google.Rpc.Status? GetRpcStatus(this Metadata metadata)
{
var entry = metadata.LastOrDefault(t => t.Key == StatusDetailsTrailerName);
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
if (entry is null)
{
return null;
}
try
{
return Google.Rpc.Status.Parser.ParseFrom(entry.ValueBytes);
}
catch
{
// If the message is malformed, just report there's no information.
return null;
tonydnewell marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <summary>
/// Add <see cref="Google.Rpc.Status"/> to the metadata.
/// Any existing status in the metadata will be overwritten.
/// </summary>
/// <param name="metadata"></param>
/// <param name="status">Status to add</param>
public static void SetRpcStatus(this Metadata metadata, Google.Rpc.Status status)
{
var entry = metadata.Get(StatusDetailsTrailerName);
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
while (entry is not null)
{
metadata.Remove(entry);
entry = metadata.Get(StatusDetailsTrailerName);
}
metadata.Add(StatusDetailsTrailerName, status.ToByteArray());
}
}