Skip to content

Commit 175e731

Browse files
authored
Merge pull request #55 from PandaTechAM/development
adding error handler for required parameter missing case
2 parents ff8c596 + c151655 commit 175e731

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/ResponseCrafter/ExceptionHandlers/Http/ApiExceptionHandler.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ public async ValueTask<bool> TryHandleAsync(HttpContext httpContext,
5050
case GridifyMapperException gridifyMapperException:
5151
await HandleGridifyExceptionMapperAsync(httpContext, gridifyMapperException, cancellationToken);
5252
break;
53+
case BadHttpRequestException badHttpRequestException:
54+
await HandleBadHttpRequestExceptionAsync(httpContext, badHttpRequestException, cancellationToken);
55+
break;
5356

5457
case ApiException apiException:
5558
await HandleApiExceptionAsync(httpContext, apiException, cancellationToken);
@@ -90,6 +93,22 @@ private async Task HandleImportExceptionAsync(HttpContext httpContext,
9093
}
9194
}
9295

96+
private async Task HandleBadHttpRequestExceptionAsync(HttpContext httpContext,
97+
BadHttpRequestException badHttpRequestException,
98+
CancellationToken cancellationToken)
99+
{
100+
if (badHttpRequestException.InnerException is System.Text.Json.JsonException jsonEx
101+
&& jsonEx.Message.ToLower().Contains("missing required properties including"))
102+
{
103+
var exception = new BadRequestException(jsonEx.Message);
104+
await HandleApiExceptionAsync(httpContext, exception, cancellationToken);
105+
}
106+
else
107+
{
108+
var exception = new BadRequestException("Bad request. Possibly malformed JSON");
109+
await HandleApiExceptionAsync(httpContext, exception, cancellationToken);
110+
}
111+
}
93112

94113
private async Task HandleGridifyExceptionAsync(HttpContext httpContext,
95114
GridifyException gridifyException,

src/ResponseCrafter/ResponseCrafter.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<Copyright>MIT</Copyright>
99
<PackageIcon>pandatech.png</PackageIcon>
1010
<PackageReadmeFile>Readme.md</PackageReadmeFile>
11-
<Version>5.1.4</Version>
11+
<Version>5.1.5</Version>
1212
<PackageId>Pandatech.ResponseCrafter</PackageId>
1313
<PackageTags>Pandatech, library, exception handler, exception, middleware, Api response</PackageTags>
1414
<Title>ResponseCrafter</Title>
1515
<Description>Handling exceptions, custom Dtos.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-response-crafter</RepositoryUrl>
17-
<PackageReleaseNotes>Adding error handler for GridifyMapperException</PackageReleaseNotes>
17+
<PackageReleaseNotes>Adding error handler for required parameter missing</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>

0 commit comments

Comments
 (0)