-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathDetermineStatusCodeMiddleware.cs
More file actions
42 lines (36 loc) · 1.72 KB
/
DetermineStatusCodeMiddleware.cs
File metadata and controls
42 lines (36 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Immutable;
using System.Net;
using Azure.DataApiBuilder.Service.Exceptions;
using HotChocolate.Execution;
/// <summary>
/// The VerifyResultMiddleware inspects the IExecutionResult created by HotChocolate
/// and determines the appropriate HTTP error code to return based on the errors in the result.
/// By Default, without this serializer, HotChocolate will return a 500 status code when database errors
/// exist. However, there is a specific error code we check for that should return a 400 status code:
/// - DatabaseInputError. This indicates that the client can make a change to request contents to influence
/// a change in the response.
/// </summary>
public sealed class DetermineStatusCodeMiddleware(RequestDelegate next)
{
private const string ERROR_CODE = nameof(DataApiBuilderException.SubStatusCodes.DatabaseInputError);
public async ValueTask InvokeAsync(RequestContext context)
{
await next(context).ConfigureAwait(false);
if (context.Result is OperationResult { Errors.Count: > 0 } singleResult)
{
if (singleResult.Errors.Any(static error => error.Code == ERROR_CODE))
{
ImmutableDictionary<string, object?>.Builder contextData =
ImmutableDictionary.CreateBuilder<string, object?>();
if (singleResult.ContextData is not null)
{
contextData.AddRange(singleResult.ContextData);
}
contextData[ExecutionContextData.HttpStatusCode] = HttpStatusCode.BadRequest;
singleResult.ContextData = contextData.ToImmutable();
}
}
}
}