Browse Source

Fix error handling.

pull/1039/head
Sebastian 3 years ago
parent
commit
c986d0d44f
  1. 4
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLResolver.cs
  2. 35
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ErrorProvider.cs
  3. 7
      backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ErrorVisitor.cs
  4. 12
      backend/src/Squidex/Config/Web/WebServices.cs
  5. 48
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs
  6. 16
      backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLSubscriptionTests.cs

4
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/CachingGraphQLResolver.cs

@ -55,9 +55,7 @@ public sealed class CachingGraphQLResolver : IConfigureExecution
options.Schema = await GetSchemaAsync(context.App);
options.HandleError(serviceProvider);
var a = await next(options);
return a;
return await next(options);
}
public async Task<GraphQLSchema> GetSchemaAsync(IAppEntity app)

35
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ErrorProvider.cs

@ -1,35 +0,0 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using GraphQL;
using GraphQL.Execution;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Validation;
namespace Squidex.Domain.Apps.Entities.Contents.GraphQL.Types;
public sealed class ErrorProvider : ErrorInfoProvider
{
public override ErrorInfo GetInfo(ExecutionError executionError)
{
var actual = base.GetInfo(executionError);
if (executionError.InnerException is ValidationException or DomainException)
{
if (!string.IsNullOrWhiteSpace(actual.Message))
{
actual.Message = $"{actual.Message} - {executionError.InnerException.Message}";
}
else
{
actual.Message = executionError.InnerException.Message;
}
}
return actual;
}
}

7
backend/src/Squidex.Domain.Apps.Entities/Contents/GraphQL/Types/ErrorVisitor.cs

@ -33,6 +33,13 @@ internal static class ErrorVisitor
log.LogError(context.OriginalException, "Failed to resolve execute query.");
}
if (context.OriginalException is ValidationException or DomainException)
{
var message = context.OriginalException.Message;
context.ErrorMessage = context.OriginalException.Message;
}
return Task.CompletedTask;
};
}

12
backend/src/Squidex/Config/Web/WebServices.cs

@ -109,21 +109,9 @@ public static class WebServices
services.AddGraphQL(builder =>
{
builder.UseApolloTracing();
builder.AddErrorInfoProvider<ErrorProvider>();
builder.AddSchema<DummySchema>();
builder.AddSystemTextJson();
builder.AddDataLoader();
builder.ConfigureExecutionOptions(options =>
{
var logger = options.RequestServices!.GetRequiredService<ILogger<GraphQLHttpMiddleware>>();
options.UnhandledExceptionDelegate = ctx =>
{
logger.LogError(ctx.Exception, "GraphQL error in field {field}", ctx.FieldContext?.FieldAst?.Name);
return Task.CompletedTask;
};
});
});
services.AddSingletonAs<DummySchema>()

48
backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLMutationTests.cs

@ -60,6 +60,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"createMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -200,6 +208,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"updateMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -291,6 +307,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"upsertMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -387,6 +411,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"patchMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -482,6 +514,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"changeMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -660,6 +700,14 @@ public class GraphQLMutationTests : GraphQLTestBase
path = new[]
{
"deleteMySchemaContent"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},

16
backend/tests/Squidex.Domain.Apps.Entities.Tests/Contents/GraphQL/GraphQLSubscriptionTests.cs

@ -95,6 +95,14 @@ public class GraphQLSubscriptionTests : GraphQLTestBase
path = new[]
{
"assetChanges"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},
@ -188,6 +196,14 @@ public class GraphQLSubscriptionTests : GraphQLTestBase
path = new[]
{
"contentChanges"
},
extensions = new
{
code = "DOMAIN_FORBIDDEN",
codes = new[]
{
"DOMAIN_FORBIDDEN"
}
}
}
},

Loading…
Cancel
Save