Skip to content

Commit dbb1e1f

Browse files
Merge pull request #188 from AntonioFalcao/feature/exception-handler
Feature/exception handler
2 parents 6980069 + 71bbf4f commit dbb1e1f

10 files changed

Lines changed: 52 additions & 20 deletions

File tree

Directory.Build.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
<PropertyGroup Label="Packages dependency versions">
99

1010
<!--Microsoft-->
11-
<Microsoft_Extensions_Version>6.0.0-preview.2.21154.6</Microsoft_Extensions_Version>
11+
<Microsoft_Extensions_Version>6.0.0-preview.3.21201.4</Microsoft_Extensions_Version>
1212

1313
<!--EF Core-->
14-
<Microsoft_EntityFrameworkCore_Version>6.0.0-preview.2.21154.2</Microsoft_EntityFrameworkCore_Version>
14+
<Microsoft_EntityFrameworkCore_Version>6.0.0-preview.3.21201.2</Microsoft_EntityFrameworkCore_Version>
1515
<Microsoft_EntityFrameworkCore_DependencyInjection_Version>6.0.0-preview.2.21154.6</Microsoft_EntityFrameworkCore_DependencyInjection_Version>
16-
<Microsoft_HealthChecks_EntityFrameworkCore_Version>6.0.0-preview.2.21154.6</Microsoft_HealthChecks_EntityFrameworkCore_Version>
16+
<Microsoft_HealthChecks_EntityFrameworkCore_Version>6.0.0-preview.3.21201.13</Microsoft_HealthChecks_EntityFrameworkCore_Version>
1717

1818
<!--GraphQL-->
1919
<GraphQL_Server_Version>5.0.1</GraphQL_Server_Version>
20-
<GraphQL_Client_Version>3.2.2</GraphQL_Client_Version>
20+
<GraphQL_Client_Version>3.2.3</GraphQL_Client_Version>
2121
<GraphQL_Version>4.2.0</GraphQL_Version>
2222

2323
<!--AutoMapper-->
@@ -31,7 +31,7 @@
3131
<HealthChecks_Version>5.0.1</HealthChecks_Version>
3232

3333
<!--Others-->
34-
<FluentValidation_Version>10.0.1</FluentValidation_Version>
34+
<FluentValidation_Version>10.0.2</FluentValidation_Version>
3535
<Scrutor_Version>3.3.0</Scrutor_Version>
3636

3737
</PropertyGroup>

src/Dotnet6.GraphQL4.CrossCutting/Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Dotnet6.GraphQL4.CrossCutting
77
{
88
public static class Application
99
{
10-
public static string Prefix { get; } = Assembly.GetEntryAssembly()?.FullName?.Substring(0, 16);
10+
public static string Prefix { get; } = Assembly.GetEntryAssembly()?.FullName?[..16];
1111

1212
public static IEnumerable<Assembly> Assemblies { get; } =
1313
DependencyContext.Default.RuntimeLibraries

src/Dotnet6.GraphQL4.CrossCutting/Extensions/DependencyInjection/ServiceCollectionExtensions.cs renamed to src/Dotnet6.GraphQL4.CrossCutting/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Dotnet6.GraphQL4.CrossCutting.Notifications;
22
using Microsoft.Extensions.DependencyInjection;
33

4-
namespace Dotnet6.GraphQL4.CrossCutting.Extensions.DependencyInjection
4+
namespace Dotnet6.GraphQL4.CrossCutting.DependencyInjection.Extensions
55
{
66
public static class ServiceCollectionExtensions
77
{

src/Dotnet6.GraphQL4.Domain.Abstractions/Extensions/DependencyInjection/ServiceCollectionExtensions.cs renamed to src/Dotnet6.GraphQL4.Domain.Abstractions/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.Extensions.DependencyInjection;
44
using Scrutor;
55

6-
namespace Dotnet6.GraphQL4.Domain.Abstractions.Extensions.DependencyInjection
6+
namespace Dotnet6.GraphQL4.Domain.Abstractions.DependencyInjection.Extensions
77
{
88
public static class ServiceCollectionExtensions
99
{

src/Dotnet6.GraphQL4.Services.Abstractions/Extensions/DependencyInjection/ServiceCollectionExtensions.cs renamed to src/Dotnet6.GraphQL4.Services.Abstractions/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Scrutor;
66

7-
namespace Dotnet6.GraphQL4.Services.Abstractions.Extensions.DependencyInjection
7+
namespace Dotnet6.GraphQL4.Services.Abstractions.DependencyInjection.Extensions
88
{
99
public static class ServiceCollectionExtensions
1010
{

src/Dotnet6.GraphQL4.Store.Repositories/Dotnet6.GraphQL4.Store.Repositories.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
<ItemGroup>
44
<PackageReference Include="Bogus" Version="33.0.2" />
5-
<PackageReference Include="EntityFramework" Version="$(Microsoft_EntityFrameworkCore_Version)" />
65
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(Microsoft_EntityFrameworkCore_Version)" />
76
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="$(Microsoft_EntityFrameworkCore_Version)" />
87
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(Microsoft_EntityFrameworkCore_Version)" />

src/Dotnet6.GraphQL4.Store.WebAPI/Graphs/Extensions/DependencyInjection/ApplicationBuilderExtensions.cs renamed to src/Dotnet6.GraphQL4.Store.WebAPI/DependencyInjection/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using GraphQL.Types;
33
using Microsoft.AspNetCore.Builder;
44

5-
namespace Dotnet6.GraphQL4.Store.WebAPI.Graphs.Extensions.DependencyInjection
5+
namespace Dotnet6.GraphQL4.Store.WebAPI.DependencyInjection.Extensions
66
{
77
public static class ApplicationBuilderExtensions
88
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Text.Json.Serialization;
2+
using GraphQL;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Diagnostics;
5+
using Microsoft.AspNetCore.Http;
6+
using Serilog;
7+
8+
namespace Dotnet6.GraphQL4.Store.WebAPI.DependencyInjection.Extensions
9+
{
10+
public static class ExceptionHandlerExtensions
11+
{
12+
public static IApplicationBuilder UseApplicationExceptionHandler(this IApplicationBuilder app)
13+
=> app.UseExceptionHandler(builder
14+
=> builder.Run(async httpContext =>
15+
{
16+
var exception = httpContext.Features.Get<IExceptionHandlerFeature>()?.Error;
17+
18+
Log.Logger.Error("[Exception Handler] {Error}", exception?.Message);
19+
20+
await httpContext.Response.WriteAsJsonAsync(
21+
value: new ExecutionResult().AddError(new(exception?.Message)),
22+
options: new() {DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull},
23+
cancellationToken: httpContext.RequestAborted);
24+
}));
25+
}
26+
}

src/Dotnet6.GraphQL4.Store.WebAPI/Graphs/Extensions/DependencyInjection/ServiceCollectionExtensions.cs renamed to src/Dotnet6.GraphQL4.Store.WebAPI/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
4+
using Dotnet6.GraphQL4.Store.WebAPI.Graphs;
45
using Dotnet6.GraphQL4.Store.WebAPI.Graphs.Executers;
56
using GraphQL;
67
using GraphQL.Server;
78
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Extensions.Logging;
9+
using Serilog;
910

10-
namespace Dotnet6.GraphQL4.Store.WebAPI.Graphs.Extensions.DependencyInjection
11+
namespace Dotnet6.GraphQL4.Store.WebAPI.DependencyInjection.Extensions
1112
{
1213
public static class ServiceCollectionExtensions
1314
{
@@ -22,11 +23,15 @@ public static IGraphQLBuilder AddApplicationGraphQL(this IServiceCollection serv
2223
.AddScoped<IDocumentExecuter, StoreDocumentExecuter>()
2324
.AddSingleton<StoreSchema>()
2425
.AddGraphQL(
25-
(options, provider) =>
26+
(options, _) =>
2627
{
2728
options.EnableMetrics = Options.IsDevelopment;
28-
var logger = provider.GetRequiredService<ILogger<Startup>>();
29-
options.UnhandledExceptionDelegate = ctx => logger.LogError("{Error} occured", ctx.OriginalException.Message);
29+
30+
options.UnhandledExceptionDelegate = exceptionContext
31+
=> Log.Error(
32+
messageTemplate: "Unhandled error occured: {Error}",
33+
propertyValue: exceptionContext.OriginalException.Message);;
34+
3035
options.ComplexityConfiguration = new() {MaxDepth = 15};
3136
})
3237
.AddSystemTextJson(

src/Dotnet6.GraphQL4.Store.WebAPI/Startup.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Linq;
2+
using Dotnet6.GraphQL4.CrossCutting.DependencyInjection.Extensions;
3+
using Dotnet6.GraphQL4.Domain.Abstractions.DependencyInjection.Extensions;
24
using Dotnet6.GraphQL4.Store.Repositories.Contexts;
35
using Dotnet6.GraphQL4.Store.WebAPI.Extensions.EndpointRouteBuilders;
4-
using Dotnet6.GraphQL4.Store.WebAPI.Graphs.Extensions.DependencyInjection;
5-
using Dotnet6.GraphQL4.CrossCutting.Extensions.DependencyInjection;
6-
using Dotnet6.GraphQL4.Domain.Abstractions.Extensions.DependencyInjection;
76
using Dotnet6.GraphQL4.Repositories.Abstractions.DependencyInjection.Extensions;
8-
using Dotnet6.GraphQL4.Services.Abstractions.Extensions.DependencyInjection;
7+
using Dotnet6.GraphQL4.Services.Abstractions.DependencyInjection.Extensions;
98
using Dotnet6.GraphQL4.Store.Repositories.DependencyInjection.Extensions;
9+
using Dotnet6.GraphQL4.Store.WebAPI.DependencyInjection.Extensions;
1010
using Dotnet6.GraphQL4.Store.WebAPI.Graphs;
1111
using HealthChecks.UI.Client;
1212
using Microsoft.AspNetCore.Builder;
@@ -42,6 +42,8 @@ public void Configure(IApplicationBuilder app , ILoggerFactory loggerFactory)
4242

4343
loggerFactory.AddSerilog();
4444

45+
app.UseApplicationExceptionHandler();
46+
4547
app.UseSerilogRequestLogging()
4648
.UseApplicationGraphQL<StoreSchema>()
4749
.UseRouting()

0 commit comments

Comments
 (0)