Skip to content

Commit 29aadf3

Browse files
committed
Fixes
1 parent 4b5e58d commit 29aadf3

File tree

6 files changed

+66
-41
lines changed

6 files changed

+66
-41
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<PackageVersion Include="CommandLineParser" Version="2.5.0" />
5959
<PackageVersion Include="Google.Api.CommonProtos" Version="2.13.0" />
6060
<PackageVersion Include="Google.Apis.Auth" Version="1.46.0" />
61-
<PackageVersion Include="Google.Protobuf" Version="3.24.0" />
61+
<PackageVersion Include="Google.Protobuf" Version="3.25.0" />
6262
<PackageVersion Include="Microsoft.Build.Locator" Version="1.5.5" />
6363
<PackageVersion Include="Microsoft.Build" Version="16.9.0" />
6464
<PackageVersion Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0-preview.23472.1" />

examples/Error/Client/Client.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<ItemGroup>
99
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Client" Link="Protos\greet.proto" />
1010

11-
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
12-
<PackageReference Include="Grpc.Tools" Version="$(GrpcToolsPackageVersion)" PrivateAssets="All" />
11+
<PackageReference Include="Google.Protobuf" />
12+
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

examples/Error/Client/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using Greet;
2121
using Grpc.Core;
2222
using Grpc.Net.Client;
23-
using Grpc.StatusProto;
2423

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

47-
var badRequest = ex.GetRpcStatus()?.GetStatusDetail<BadRequest>();
46+
var badRequest = ex.GetRpcStatus()?.GetDetail<BadRequest>();
4847
if (badRequest != null)
4948
{
5049
foreach (var fieldViolation in badRequest.FieldViolations)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#region Copyright notice and license
2+
3+
// Copyright 2019 The gRPC Authors
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
#endregion
18+
19+
using System.Runtime.CompilerServices;
20+
using Google.Protobuf.WellKnownTypes;
21+
using Google.Rpc;
22+
using Grpc.Core;
23+
24+
namespace Server;
25+
26+
public static class GrpcValidation
27+
{
28+
public static void ArgumentNotNullOrEmpty(string value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
29+
{
30+
if (string.IsNullOrEmpty(value))
31+
{
32+
var status = new Google.Rpc.Status
33+
{
34+
Code = (int)Code.InvalidArgument,
35+
Message = "Bad request",
36+
Details =
37+
{
38+
Any.Pack(new BadRequest
39+
{
40+
FieldViolations =
41+
{
42+
new BadRequest.Types.FieldViolation
43+
{
44+
Field = paramName,
45+
Description = "Value is null or empty"
46+
}
47+
}
48+
})
49+
}
50+
};
51+
throw status.ToRpcException();
52+
}
53+
}
54+
}

examples/Error/Server/Server.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<ItemGroup>
88
<Protobuf Include="..\Proto\greet.proto" GrpcServices="Server" Link="Protos\greet.proto" />
99

10-
<PackageReference Include="Google.Protobuf" Version="$(GoogleProtobufPackageVersion)" />
11-
<PackageReference Include="Grpc.Tools" Version="$(GrpcToolsPackageVersion)" />
10+
<PackageReference Include="Google.Protobuf" />
11+
<PackageReference Include="Grpc.Tools" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

examples/Error/Server/Services/GreeterService.cs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,15 @@
2121
using Google.Rpc;
2222
using Greet;
2323
using Grpc.Core;
24-
using Grpc.StatusProto;
2524

26-
namespace Server
25+
namespace Server;
26+
27+
public class GreeterService : Greeter.GreeterBase
2728
{
28-
public class GreeterService : Greeter.GreeterBase
29+
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
2930
{
30-
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
31-
{
32-
ArgumentNotNullOrEmpty(request.Name);
33-
34-
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
35-
}
31+
GrpcValidation.ArgumentNotNullOrEmpty(request.Name);
3632

37-
private static void ArgumentNotNullOrEmpty(string value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
38-
{
39-
if (string.IsNullOrEmpty(value))
40-
{
41-
throw new Google.Rpc.Status
42-
{
43-
Code = (int)Code.InvalidArgument,
44-
Message = "Bad request",
45-
Details =
46-
{
47-
Any.Pack(new BadRequest
48-
{
49-
FieldViolations =
50-
{
51-
new BadRequest.Types.FieldViolation
52-
{
53-
Field = paramName,
54-
Description = "Value is null or empty"
55-
}
56-
}
57-
})
58-
}
59-
}.ToRpcException();
60-
}
61-
}
33+
return Task.FromResult(new HelloReply { Message = "Hello " + request.Name });
6234
}
6335
}

0 commit comments

Comments
 (0)