From 29aadf39bac7316b171d7bf1ae3464a43707965c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Sat, 6 Jan 2024 12:42:40 +0800 Subject: [PATCH] Fixes --- Directory.Packages.props | 2 +- examples/Error/Client/Client.csproj | 4 +- examples/Error/Client/Program.cs | 3 +- examples/Error/Server/GrpcValidation.cs | 54 +++++++++++++++++++ examples/Error/Server/Server.csproj | 4 +- .../Error/Server/Services/GreeterService.cs | 40 +++----------- 6 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 examples/Error/Server/GrpcValidation.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index e5914c97a..194893368 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -58,7 +58,7 @@ - + diff --git a/examples/Error/Client/Client.csproj b/examples/Error/Client/Client.csproj index 6b5421801..dad22d467 100644 --- a/examples/Error/Client/Client.csproj +++ b/examples/Error/Client/Client.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/examples/Error/Client/Program.cs b/examples/Error/Client/Program.cs index 3021039b3..e5fe5bd82 100644 --- a/examples/Error/Client/Program.cs +++ b/examples/Error/Client/Program.cs @@ -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); @@ -44,7 +43,7 @@ { Console.WriteLine($"Server error: {ex.Status.Detail}"); - var badRequest = ex.GetRpcStatus()?.GetStatusDetail(); + var badRequest = ex.GetRpcStatus()?.GetDetail(); if (badRequest != null) { foreach (var fieldViolation in badRequest.FieldViolations) diff --git a/examples/Error/Server/GrpcValidation.cs b/examples/Error/Server/GrpcValidation.cs new file mode 100644 index 000000000..b9ed5d48e --- /dev/null +++ b/examples/Error/Server/GrpcValidation.cs @@ -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(); + } + } +} diff --git a/examples/Error/Server/Server.csproj b/examples/Error/Server/Server.csproj index c35044e3c..c0c980d31 100644 --- a/examples/Error/Server/Server.csproj +++ b/examples/Error/Server/Server.csproj @@ -7,8 +7,8 @@ - - + + diff --git a/examples/Error/Server/Services/GreeterService.cs b/examples/Error/Server/Services/GreeterService.cs index a9e9adbb3..1a26cc9d8 100644 --- a/examples/Error/Server/Services/GreeterService.cs +++ b/examples/Error/Server/Services/GreeterService.cs @@ -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 SayHello(HelloRequest request, ServerCallContext context) { - public override Task 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 }); } }