Skip to content

Exceptions thrown in type converters are not surfaced #7404

@cmeeren

Description

@cmeeren

Product

Hot Chocolate

Version

14.0.0

Link to minimal reproduction

See zip below

Steps to reproduce

Repro solution: HotChocolateBugRepro.zip

Code for quick reference:

var builder = WebApplication.CreateBuilder(args);
builder.Services
    .AddGraphQLServer()
    .AddQueryType<Query>()
    .AddTypeConverter<string, UserId>(UserId.Parse)
    .AddTypeConverter<UserId, string>(id => id.Value.ToString())
    .BindRuntimeType<UserId, StringType>();
var app = builder.Build();
app.MapGraphQL();
app.Run();

public record UserId(int Value)
{
    public static UserId Parse(string id)
    {
        if (Int32.TryParse(id, out int result))
        {
            return new UserId(result);
        }

        throw new SerializationException("User ID must be an integer", new StringType());
    }
}

public class Query
{
    public string Test(UserId arg) => "";
}

Run this query:

query {
  test(arg: "invalid")
}

What is expected?

An error containing the message User ID must be an integer. Furthermore, the error, being targeted to clients, should not in any way reference the type name UserId (which is an internal Server implementation detail and not part of the schema).

What is actually happening?

The following error, which:

  • does not use my supplied error message
  • references the internal type name in both message and extensions.requestedType
  • is generally clearly oriented to server implementors
{
  "errors": [
    {
      "message": "Unable to convert the value of the argument `arg` to `UserId`. Check if the requested type is correct or register a custom type converter.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "test"
      ],
      "extensions": {
        "fieldName": "test",
        "argumentName": "arg",
        "requestedType": "UserId"
      }
    }
  ],
  "data": {
    "test": null
  }
}

Relevant log output

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions