Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 6, 2024
1 parent 4b5e58d commit 29aadf3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<PackageVersion Include="CommandLineParser" Version="2.5.0" />
<PackageVersion Include="Google.Api.CommonProtos" Version="2.13.0" />
<PackageVersion Include="Google.Apis.Auth" Version="1.46.0" />
<PackageVersion Include="Google.Protobuf" Version="3.24.0" />
<PackageVersion Include="Google.Protobuf" Version="3.25.0" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.5.5" />
<PackageVersion Include="Microsoft.Build" Version="16.9.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview.23472.1" />
Expand Down
4 changes: 2 additions & 2 deletions examples/Error/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<ItemGroup>
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />

<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
<PackageReference Include="Grpc.Tools" Version="$(GrpcToolsPackageVersion)" PrivateAssets="All" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions examples/Error/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Greet;
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.StatusProto;

using var channel = GrpcChannel.ForAddress("https://localhost:5001");
var client = new Greeter.GreeterClient(channel);
Expand All @@ -44,7 +43,7 @@
{
Console.WriteLine($"Server error: {ex.Status.Detail}");

var badRequest = ex.GetRpcStatus()?.GetStatusDetail<BadRequest>();
var badRequest = ex.GetRpcStatus()?.GetDetail<BadRequest>();
if (badRequest != null)
{
foreach (var fieldViolation in badRequest.FieldViolations)
Expand Down
54 changes: 54 additions & 0 deletions examples/Error/Server/GrpcValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#region Copyright notice and license

// Copyright 2019 The 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.

#endregion

using System.Runtime.CompilerServices;
using Google.Protobuf.WellKnownTypes;
using Google.Rpc;
using Grpc.Core;

namespace Server;

public static class GrpcValidation
{
public static void ArgumentNotNullOrEmpty(string value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
{
if (string.IsNullOrEmpty(value))
{
var status = new Google.Rpc.Status
{
Code = (int)Code.InvalidArgument,
Message = "Bad request",
Details =
{
Any.Pack(new BadRequest
{
FieldViolations =
{
new BadRequest.Types.FieldViolation
{
Field = paramName,
Description = "Value is null or empty"
}
}
})
}
};
throw status.ToRpcException();
}
}
}
4 changes: 2 additions & 2 deletions examples/Error/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ItemGroup>
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />

<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
<PackageReference Include="Grpc.Tools" Version="$(GrpcToolsPackageVersion)" />
<PackageReference Include="Google.Protobuf" />
<PackageReference Include="Grpc.Tools" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 6 additions & 34 deletions examples/Error/Server/Services/GreeterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,15 @@
using Google.Rpc;
using Greet;
using Grpc.Core;
using Grpc.StatusProto;

namespace Server
namespace Server;

public class GreeterService : Greeter.GreeterBase
{
public class GreeterService : Greeter.GreeterBase
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
ArgumentNotNullOrEmpty(request.Name);

return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
}
GrpcValidation.ArgumentNotNullOrEmpty(request.Name);

private static void ArgumentNotNullOrEmpty(string value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
{
if (string.IsNullOrEmpty(value))
{
throw new Google.Rpc.Status
{
Code = (int)Code.InvalidArgument,
Message = "Bad request",
Details =
{
Any.Pack(new BadRequest
{
FieldViolations =
{
new BadRequest.Types.FieldViolation
{
Field = paramName,
Description = "Value is null or empty"
}
}
})
}
}.ToRpcException();
}
}
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
}
}

0 comments on commit 29aadf3

Please sign in to comment.