-
Notifications
You must be signed in to change notification settings - Fork 773
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
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
85f7a57
First pass at Grpc.StatusProto API
tonydnewell ef2ec28
Updated README.md
tonydnewell 83d11ef
Update from review comments
tonydnewell 1a94ae0
Update from review comments
tonydnewell 5314bd6
Fix tests on net472
tonydnewell 3b57cac
Fix framework tests
tonydnewell f88a84c
Update from review comments
tonydnewell a032c7a
Merge branch 'grpc:master' into grpc.statuspro
tonydnewell 46e482a
Add cache to RpcStatusExtensions
tonydnewell d5449f5
Update from review comments
tonydnewell 5236b2e
Added TODO comment to StandardErrorTypeRegistry
tonydnewell 9b94477
Update src/Grpc.StatusProto/README.md
tonydnewell 956ad47
Merge branch 'grpc:master' into grpc.statuspro
tonydnewell d8a9228
Just merging from master
tonydnewell b052f31
Updating from review
tonydnewell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// 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.Rpc; | ||
using Grpc.Shared; | ||
|
||
namespace Grpc.StatusProto; | ||
|
||
/// <summary> | ||
/// Extensions methods for <see cref="System.Exception"/> | ||
/// </summary> | ||
public static class ExceptionExtensions | ||
{ | ||
/// <summary> | ||
/// Create a <see cref="Google.Rpc.DebugInfo"/> from an <see cref="System.Exception"/>, | ||
/// populating the Message and StackTrace from the exception. | ||
/// Note: experimental API that can change or be removed without any prior notice. | ||
/// </summary> | ||
/// <remarks> | ||
/// <example> | ||
/// For example: | ||
/// <code> | ||
/// try { /* ... */ | ||
/// } | ||
/// catch (Exception e) { | ||
/// Google.Rpc.Status status = new() { | ||
/// Code = (int)StatusCode.Internal, | ||
/// Message = "Internal error", | ||
/// Details = { | ||
/// // populate debugInfo from the exception | ||
/// Any.Pack(e.ToRpcDebugInfo()) | ||
/// } | ||
/// }; | ||
/// // ... | ||
/// } | ||
/// </code> | ||
/// </example> | ||
/// </remarks> | ||
/// <param name="exception"></param> | ||
/// <param name="innerDepth">Maximum number of inner exceptions to include in the StackTrace. Defaults | ||
/// to not including any inner exceptions</param> | ||
/// <returns> | ||
/// A new <see cref="Google.Rpc.DebugInfo"/> populated from the exception. | ||
/// </returns> | ||
public static DebugInfo ToRpcDebugInfo(this Exception exception, int innerDepth = 0) | ||
tonydnewell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ArgumentNullThrowHelper.ThrowIfNull(exception); | ||
|
||
var debugInfo = new DebugInfo(); | ||
|
||
var message = exception.Message; | ||
var name = exception.GetType().FullName; | ||
|
||
// Populate the Detail from the exception type and message | ||
debugInfo.Detail = message is null ? name : name + ": " + message; | ||
|
||
// Populate the StackEntries from the exception StackTrace | ||
if (exception.StackTrace is not null) | ||
{ | ||
var sr = new StringReader(exception.StackTrace); | ||
var entry = sr.ReadLine(); | ||
while (entry is not null) | ||
{ | ||
debugInfo.StackEntries.Add(entry); | ||
entry = sr.ReadLine(); | ||
} | ||
} | ||
|
||
// Add inner exceptions to the StackEntries | ||
var inner = exception.InnerException; | ||
while (innerDepth > 0 && inner is not null) | ||
{ | ||
message = inner.Message; | ||
name = inner.GetType().FullName; | ||
debugInfo.StackEntries.Add("InnerException: " + (message is null ? name : name + ": " + message)); | ||
|
||
if (inner.StackTrace is not null) | ||
{ | ||
var sr = new StringReader(inner.StackTrace); | ||
var entry = sr.ReadLine(); | ||
while (entry is not null) | ||
{ | ||
debugInfo.StackEntries.Add(entry); | ||
entry = sr.ReadLine(); | ||
} | ||
} | ||
|
||
inner = inner.InnerException; | ||
--innerDepth; | ||
} | ||
|
||
return debugInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>gRPC C# API for error handling using google/rpc/status.proto</Description> | ||
<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="$(GoogleApiCommonProtosPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="README.md" Pack="true" PackagePath="\" /> | ||
|
||
<Compile Include="..\Shared\NullableAttributes.cs" Link="Internal\NullableAttributes.cs" /> | ||
<Compile Include="..\Shared\CallerArgumentExpressionAttribute.cs" Link="Internal\CallerArgumentExpressionAttribute.cs" /> | ||
<Compile Include="..\Shared\ThrowHelpers\ArgumentNullThrowHelper.cs" Link="Internal\ArgumentNullThrowHelper.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Grpc.Core.Api\Grpc.Core.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// 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; | ||
using Grpc.Shared; | ||
|
||
namespace Grpc.StatusProto; | ||
|
||
/// <summary> | ||
/// Extension methods for the Grpc.Core.Metadata | ||
/// </summary> | ||
public static class MetadataExtensions | ||
{ | ||
/// <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. | ||
/// Note: experimental API that can change or be removed without any prior notice. | ||
/// </summary> | ||
/// <param name="metadata"></param> | ||
/// <param name="throwOnParseError">if true then <see cref="Google.Protobuf.InvalidProtocolBufferException"/> | ||
/// is thrown if the metadata cannot be parsed. Otherwise null is returned on a parsing error.</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, bool throwOnParseError = false) | ||
{ | ||
ArgumentNullThrowHelper.ThrowIfNull(metadata); | ||
|
||
var entry = metadata.Get(StatusDetailsTrailerName); | ||
if (entry is null) | ||
{ | ||
return null; | ||
} | ||
try | ||
{ | ||
return Google.Rpc.Status.Parser.ParseFrom(entry.ValueBytes); | ||
} | ||
catch when (!throwOnParseError) | ||
{ | ||
// By default 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. | ||
/// Note: experimental API that can change or be removed without any prior notice. | ||
/// </summary> | ||
/// <param name="metadata"></param> | ||
/// <param name="status">Status to add</param> | ||
public static void SetRpcStatus(this Metadata metadata, Google.Rpc.Status status) | ||
{ | ||
ArgumentNullThrowHelper.ThrowIfNull(metadata); | ||
ArgumentNullThrowHelper.ThrowIfNull(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()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the namespace of the extension types should change. See #2273 (comment)