Skip to content

Commit 2c58883

Browse files
Adding Exception Handler.
1 parent b916429 commit 2c58883

8 files changed

Lines changed: 47 additions & 14 deletions

File tree

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.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)