< Summary

Information
Class: Elsa.Workflows.Api.Middleware.JsonSerializationErrorHandlerMiddleware
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Middleware/JsonSerializationErrorHandlerMiddleware.cs
Line coverage
53%
Covered lines: 8
Uncovered lines: 7
Coverable lines: 15
Total lines: 38
Line coverage: 53.3%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
InvokeAsync()100%6450%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Middleware/JsonSerializationErrorHandlerMiddleware.cs

#LineLine coverage
 1using System.Text.Json;
 2using Microsoft.AspNetCore.Http;
 3
 4namespace Elsa.Workflows.Api.Middleware;
 5
 6/// <summary>
 7/// Catches JSON serialization errors during POST requests and returns Bad Request responses.
 8/// </summary>
 19public class JsonSerializationErrorHandlerMiddleware(RequestDelegate next)
 10{
 11    public async Task InvokeAsync(HttpContext httpContext)
 12    {
 23613        var method = httpContext.Request.Method;
 14
 15        // If the HTTP verb is anything but POST or PUT, do nothing.
 23616        if (!string.Equals(method, "POST", StringComparison.InvariantCultureIgnoreCase) && !string.Equals(method, "PUT",
 17        {
 2418            await next(httpContext);
 2419            return;
 20        }
 21
 22        try
 23        {
 21224            await next(httpContext);
 21225        }
 026        catch (JsonException e)
 27        {
 028            var model = new
 029            {
 030                ModelBindingError = e.Message
 031            };
 32
 033            var result = Results.BadRequest(model);
 34
 035            await result.ExecuteAsync(httpContext);
 36        }
 23637    }
 38}