Title: Difference in catch and reporting behavior of Coercing value errors vs literal errors · Issue #2811 · graphql-java/graphql-java · GitHub
Open Graph Title: Difference in catch and reporting behavior of Coercing value errors vs literal errors · Issue #2811 · graphql-java/graphql-java
X Title: Difference in catch and reporting behavior of Coercing value errors vs literal errors · Issue #2811 · graphql-java/graphql-java
Description: Describe the bug I've recently noticed a difference in behavior (using release 18.0) between how exceptions are handled implementing a custom scalar which parses input fields values and literals. As I understand them parseLiteral is used...
Open Graph Description: Describe the bug I've recently noticed a difference in behavior (using release 18.0) between how exceptions are handled implementing a custom scalar which parses input fields values and literals. A...
X Description: Describe the bug I've recently noticed a difference in behavior (using release 18.0) between how exceptions are handled implementing a custom scalar which parses input fields values and literal...
Opengraph URL: https://github.com/graphql-java/graphql-java/issues/2811
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Difference in catch and reporting behavior of Coercing value errors vs literal errors","articleBody":"**Describe the bug**\r\n\r\nI've recently noticed a difference in behavior (using release 18.0) between how exceptions are handled implementing a custom scalar which parses input fields values and literals.\r\n\r\nAs I understand them `parseLiteral` is used in the case where an input comes directly from a query. This works well [as described in the docs](https://www.graphql-java.com/documentation/scalars/) when the custom `Coercing` throws a `CoercingParseLiteralException` exception. The error is handled by the execution engine and serialized with normal graphql errors according to the spec in the responses `errors` array.\r\n\r\nHowever, when the same input comes from a query variable, `parseValue` is used and, as the [as described in the docs](https://www.graphql-java.com/documentation/scalars/) mention, I throw `CoercingParseValueException` exception. In the case the execution engine does not catch and handle the exception and instead it results in a 500 error from my server rather than a 200 response with a graphql errors spec response.\r\n\r\nbelow is an example stack trace if it helps\r\n\r\n```\r\n2022-04-23 17:27:49,743 - - - [jetty task-169] ERROR org.jooby.Err - execution of: POST/ resulted in exception\r\nRoute:\r\n | Method | Path | Source | Name | Pattern | Consumes | Produces |\r\n |--------|------|--------------------------------------------|------------|---------|----------|----------|\r\n | POST | / | com.meetup.graphql.server.GraphqlServer:66 | /anonymous | / | [*/*] | [*/*] |\r\n\r\nStacktrace:\r\norg.jooby.Err: Server Error(500)\r\n \u003csnip\u003e\r\nCaused by: graphql.schema.CoercingParseValueException: Variable 'startDateTime' has an invalid value: Try something like '2022-04-24T17:27' instead\r\n at graphql.schema.CoercingParseValueException$Builder.build(CoercingParseValueException.java:46)\r\n at graphql.execution.ValuesResolver.externalValueToInternalValueForVariables(ValuesResolver.java:420)\r\n at graphql.execution.ValuesResolver.coerceVariableValues(ValuesResolver.java:94)\r\n at graphql.analysis.QueryTraverser.coerceVariables(QueryTraverser.java:64)\r\n at graphql.analysis.QueryTraverser.\u003cinit\u003e(QueryTraverser.java:60)\r\n at graphql.analysis.QueryTraverser.\u003cinit\u003e(QueryTraverser.java:40)\r\n at graphql.analysis.QueryTraverser$Builder.build(QueryTraverser.java:297)\r\n at graphql.analysis.MaxQueryComplexityInstrumentation.newQueryTraverser(MaxQueryComplexityInstrumentation.java:134)\r\n at graphql.analysis.MaxQueryComplexityInstrumentation.lambda$beginValidation$4(MaxQueryComplexityInstrumentation.java:85)\r\n at graphql.execution.instrumentation.SimpleInstrumentationContext.onCompleted(SimpleInstrumentationContext.java:51)\r\n at graphql.execution.instrumentation.ChainedInstrumentation$ChainedInstrumentationContext.lambda$onCompleted$1(ChainedInstrumentation.java:238)\r\n at graphql.com.google.common.collect.ImmutableList.forEach(ImmutableList.java:422)\r\n at graphql.execution.instrumentation.ChainedInstrumentation$ChainedInstrumentationContext.onCompleted(ChainedInstrumentation.java:238)\r\n at graphql.GraphQL.validate(GraphQL.java:628)\r\n at graphql.GraphQL.parseAndValidate(GraphQL.java:589)\r\n at graphql.GraphQL.lambda$parseValidateAndExecute$10(GraphQL.java:553)\r\n at com.meetup.graphql.server.GraphqlServer.lambda$new$0(GraphqlServer.java:59)\r\n at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1134)\r\n at java.base/java.util.Collections$SynchronizedMap.computeIfAbsent(Collections.java:2682)\r\n at com.meetup.graphql.server.GraphqlServer.lambda$new$1(GraphqlServer.java:59)\r\n at graphql.execution.preparsed.PreparsedDocumentProvider.getDocumentAsync(PreparsedDocumentProvider.java:45)\r\n at graphql.GraphQL.parseValidateAndExecute(GraphQL.java:555)\r\n at graphql.GraphQL.executeAsync(GraphQL.java:524)\r\n at graphql.GraphQL.execute(GraphQL.java:450)\r\n at com.meetup.graphql.server.GraphqlQueryExecutor.toExecutionResult(GraphqlQueryExecutor.java:93)\r\n at com.meetup.graphql.server.GraphqlHandler.handle(GraphqlHandler.java:82)\r\n at com.meetup.graphql.server.GraphqlServer.lambda$new$2(GraphqlServer.java:79)\r\n at org.jooby.Route$Handler.handle(Route.java:1761)\r\n at org.jooby.internal.RouteImpl.handle(RouteImpl.java:280)\r\n at org.jooby.internal.RouteChain.next(RouteChain.java:262)\r\n at org.jooby.Route$Chain.next(Route.java:2164)\r\n at org.jooby.internal.HttpHandlerImpl.handle(HttpHandlerImpl.java:496)\r\n ... 19 more\r\nCaused by: java.time.format.DateTimeParseException: Text '2022-05-01T05:30+400' could not be parsed, unparsed text found at index 16\r\n at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049)\r\n at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)\r\n at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:492)\r\n```\r\n\r\n\r\n**To Reproduce**\r\n\r\nHere is what my `Coercing` scalar impl looks like. note parse value errors are reported with `CoercingParseValueException` and parse literal errors are reported with `CoercingParseLiteralException`\r\n\r\nwhen an invalid input is passed in a literal the execution engine does what I expect. when an invalid input is passed in through a variable, the exception bubbles all the way up to my server, resulting in a 500 error.\r\n\r\n```java\r\n static java.time.LocalDateTime parseLocalDateTimeValue(Object input) {\r\n String value =\r\n input instanceof StringValue ? ((StringValue) input).getValue() : Objects.toString(input);\r\n try {\r\n if (input instanceof StringValue) {\r\n return java.time.LocalDateTime.parse(value);\r\n } else if (input instanceof String) {\r\n return java.time.LocalDateTime.parse(value);\r\n } else throw new IllegalArgumentException(\"Unexpected input type: \" + input.getClass());\r\n } catch (Exception e) {\r\n throw new CoercingParseValueException(\r\n \"Try something like '\"\r\n + java.time.LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).plusDays(1)\r\n + \"' instead\",\r\n e);\r\n }\r\n }\r\n\r\n static java.time.LocalDateTime parseLocalDateTimeLiteral(Object input) {\r\n String value =\r\n input instanceof StringValue ? ((StringValue) input).getValue() : Objects.toString(input);\r\n try {\r\n if (input instanceof StringValue) {\r\n return java.time.LocalDateTime.parse(value);\r\n } else if (input instanceof String) {\r\n return java.time.LocalDateTime.parse(value);\r\n } else throw new IllegalArgumentException(\"Unexpected input type: \" + input.getClass());\r\n } catch (Exception e) {\r\n throw new CoercingParseLiteralException(\r\n \"Try something like '\"\r\n + java.time.LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).plusDays(1)\r\n + \"' instead.\",\r\n e);\r\n }\r\n }\r\n\r\n public static final GraphQLScalarType LocalDateTime =\r\n GraphQLScalarType.newScalar()\r\n .name(\"LocalDateTime\")\r\n .description(\"A custom scalar that handles timestamps that exclude timezone information\")\r\n .coercing(\r\n new Coercing\u003cjava.time.LocalDateTime, String\u003e() {\r\n @Override\r\n public String serialize(Object dataFetcherResult) {\r\n return null; // omitted as its not relevant to this issue\r\n }\r\n\r\n @Override\r\n public java.time.LocalDateTime parseValue(Object input) {\r\n return parseLocalDateTimeValue(input);\r\n }\r\n\r\n @Override\r\n public java.time.LocalDateTime parseLiteral(Object input) {\r\n return parseLocalDateTimeLiteral(input);\r\n }\r\n })\r\n .build();\r\n```\r\n\r\n\r\n\r\n","author":{"url":"https://github.com/softprops","@type":"Person","name":"softprops"},"datePublished":"2022-04-23T21:34:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/2811/graphql-java/issues/2811"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5de8040d-74ca-f703-38da-c7172622207a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DC46:2E5AC3:14ECCA:1C4080:6A5FE951 |
| html-safe-nonce | 6397a55cdec33bc901658ea8d9d27a896c2765dec9ee3613a93b3a004a419e20 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEQzQ2OjJFNUFDMzoxNEVDQ0E6MUM0MDgwOjZBNUZFOTUxIiwidmlzaXRvcl9pZCI6IjMxNDg2NTQxODQ0NzgyNzE4MjUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5ef14bf8225f27b38345d8379c23131e0d4dca85a6a68ff24f7d158d78d6c107 |
| hovercard-subject-tag | issue:1213459314 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/graphql-java/graphql-java/2811/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ded063b8e45a6e1cec4a4fafb588fb00d375ba455aabc616e1b2be6374b27c1/graphql-java/graphql-java/issues/2811 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ded063b8e45a6e1cec4a4fafb588fb00d375ba455aabc616e1b2be6374b27c1/graphql-java/graphql-java/issues/2811 |
| og:image:alt | Describe the bug I've recently noticed a difference in behavior (using release 18.0) between how exceptions are handled implementing a custom scalar which parses input fields values and literals. A... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | softprops |
| hostname | github.com |
| expected-hostname | github.com |
| None | ba296ecd3a3521238fe43b3b3b08cc09207ddc21173e16ee656db03803de8e2b |
| turbo-cache-control | no-preview |
| go-import | github.com/graphql-java/graphql-java git https://github.com/graphql-java/graphql-java.git |
| octolytics-dimension-user_id | 14289921 |
| octolytics-dimension-user_login | graphql-java |
| octolytics-dimension-repository_id | 38602457 |
| octolytics-dimension-repository_nwo | graphql-java/graphql-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38602457 |
| octolytics-dimension-repository_network_root_nwo | graphql-java/graphql-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 6e50fb37e29e4aa0df6386cb485e58b0e3ca97ab |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width