diff options
author | James Newton-King <james@newtonking.com> | 2022-05-15 03:18:29 +0300 |
---|---|---|
committer | James Newton-King <james@newtonking.com> | 2022-05-15 03:18:29 +0300 |
commit | 9acc11c2099fdf65aef59abef15da37c47b848ce (patch) | |
tree | 5e2101fa57a69189c373aeecd0a5e11dcad1f9e5 | |
parent | 6c2051663e61e9ff0fa173e0baa23091ec26f08f (diff) |
PR feedbackjamesnk/httpbody
2 files changed, 8 insertions, 8 deletions
diff --git a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonRequestHelpers.cs b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonRequestHelpers.cs index 9c6675e123..3507363c96 100644 --- a/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonRequestHelpers.cs +++ b/src/Grpc/JsonTranscoding/src/Microsoft.AspNetCore.Grpc.JsonTranscoding/Internal/JsonRequestHelpers.cs @@ -88,7 +88,7 @@ internal static class JsonRequestHelpers } } - public static async Task SendErrorResponse(HttpResponse response, Encoding encoding, Status status, JsonSerializerOptions options) + public static async ValueTask SendErrorResponse(HttpResponse response, Encoding encoding, Status status, JsonSerializerOptions options) { if (!response.HasStarted) { @@ -150,7 +150,7 @@ internal static class JsonRequestHelpers return StatusCodes.Status500InternalServerError; } - public static async Task WriteResponseMessage(HttpResponse response, Encoding encoding, object responseBody, JsonSerializerOptions options, CancellationToken cancellationToken) + public static async ValueTask WriteResponseMessage(HttpResponse response, Encoding encoding, object responseBody, JsonSerializerOptions options, CancellationToken cancellationToken) { var (stream, usesTranscodingStream) = GetStream(response.Body, encoding); @@ -167,7 +167,7 @@ internal static class JsonRequestHelpers } } - public static async Task<TRequest> ReadMessage<TRequest>(JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions) where TRequest : class + public static async ValueTask<TRequest> ReadMessage<TRequest>(JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions) where TRequest : class { try { @@ -190,7 +190,7 @@ internal static class JsonRequestHelpers if (!serverCallContext.IsJsonRequestContent) { GrpcServerLog.UnsupportedRequestContentType(serverCallContext.Logger, serverCallContext.HttpContext.Request.ContentType); - throw new InvalidOperationException("Request content-type of application/json is required."); + throw new InvalidOperationException($"Unable to read the request as JSON because the request content type '{serverCallContext.HttpContext.Request.ContentType}' is not a known JSON content type."); } var (stream, usesTranscodingStream) = GetStream(serverCallContext.HttpContext.Request.Body, serverCallContext.RequestEncoding); @@ -281,12 +281,12 @@ internal static class JsonRequestHelpers catch (JsonException ex) { GrpcServerLog.ErrorReadingMessage(serverCallContext.Logger, ex); - throw new RpcException(new Status(StatusCode.InvalidArgument, "Request JSON payload is not correctly formatted.")); + throw new RpcException(new Status(StatusCode.InvalidArgument, "Request JSON payload is not correctly formatted.", ex)); } catch (Exception ex) { GrpcServerLog.ErrorReadingMessage(serverCallContext.Logger, ex); - throw new RpcException(new Status(StatusCode.InvalidArgument, ex.Message)); + throw new RpcException(new Status(StatusCode.InvalidArgument, ex.Message, ex)); } } @@ -343,7 +343,7 @@ internal static class JsonRequestHelpers }); } - public static async Task SendMessage<TResponse>(JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions, TResponse message, CancellationToken cancellationToken) where TResponse : class + public static async ValueTask SendMessage<TResponse>(JsonTranscodingServerCallContext serverCallContext, JsonSerializerOptions serializerOptions, TResponse message, CancellationToken cancellationToken) where TResponse : class { var response = serverCallContext.HttpContext.Response; diff --git a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/UnaryServerCallHandlerTests.cs b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/UnaryServerCallHandlerTests.cs index 081e9b068d..21855fb25f 100644 --- a/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/UnaryServerCallHandlerTests.cs +++ b/src/Grpc/JsonTranscoding/test/Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests/UnaryServerCallHandlerTests.cs @@ -511,7 +511,7 @@ public class UnaryServerCallHandlerTests : LoggedTest // Assert Assert.Equal(400, httpContext.Response.StatusCode); - var expectedError = "Request content-type of application/json is required."; + var expectedError = $"Unable to read the request as JSON because the request content type '{contentType}' is not a known JSON content type."; httpContext.Response.Body.Seek(0, SeekOrigin.Begin); using var responseJson = JsonDocument.Parse(httpContext.Response.Body); Assert.Equal(expectedError, responseJson.RootElement.GetProperty("message").GetString()); |