Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private static bool TryCreateFactoryFromConstructor(
Expression.Assign(variable, createError),
previous is null
? Expression.Assign(variable, nullValue)
: Expression.Assign(variable, previous));
: previous);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using HotChocolate.Execution;
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -195,6 +196,38 @@ await Snapshot.Create()
.MatchAsync();
}

[Fact]
public async Task ErrorMiddleware_Should_MapMultipleConstructors_FirstEx()
{
// Arrange
var executor =
await BuildSchemaAsync(
() => throw new InvalidOperationException(),
field => field.Error<CustomErrorWithMultipleConstructors>());

// Act
var res = await executor.ExecuteAsync(Query);

// Assert
AssertMappedPayloadError(res, "InvalidOperationException");
}

[Fact]
public async Task ErrorMiddleware_Should_MapMultipleConstructors_SecondEx()
{
// Arrange
var executor =
await BuildSchemaAsync(
() => throw new NullReferenceException(),
field => field.Error<CustomErrorWithMultipleConstructors>());

// Act
var res = await executor.ExecuteAsync(Query);

// Assert
AssertMappedPayloadError(res, "NullReferenceException");
}

[Fact]
public async Task ErrorMiddleware_Should_MapMultipleFactories_When_InterfaceIsUsed()
{
Expand Down Expand Up @@ -366,6 +399,21 @@ public CustomNullRef CreateErrorFrom(NullReferenceException ex)
}
}

public class CustomErrorWithMultipleConstructors
{
public CustomErrorWithMultipleConstructors(InvalidOperationException _)
{
Message = "InvalidOperationException";
}

public CustomErrorWithMultipleConstructors(NullReferenceException _)
{
Message = "NullReferenceException";
}

public string Message { get; }
}

public class CustomErrorPayloadErrorFactory
: IPayloadErrorFactory<InvalidOperationException, CustomError>
, IPayloadErrorFactory<NullReferenceException, CustomNullRef>
Expand Down Expand Up @@ -429,4 +477,20 @@ public class Payload
{
public string Foo() => "Bar";
}

private static void AssertMappedPayloadError(IExecutionResult result, string expectedMessage)
{
using var document = JsonDocument.Parse(result.ToJson());

Assert.False(document.RootElement.TryGetProperty("errors", out _));

var message = document.RootElement
.GetProperty("data")
.GetProperty("throw")
.GetProperty("errors")[0]
.GetProperty("message")
.GetString();

Assert.Equal(expectedMessage, message);
}
}
Loading