Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle invalid JSON request #1795

Closed
ikenox opened this issue Mar 30, 2023 · 3 comments
Closed

Handle invalid JSON request #1795

ikenox opened this issue Mar 30, 2023 · 3 comments
Assignees

Comments

@ikenox
Copy link

ikenox commented Mar 30, 2023

Currently request with invalid JSON raises a general exception (such as RuntimeException and JsonParsingException), and it's not handled in the library.
Like an unparseable input document error, it's better to return ExecutionResult with a specific GraphQLError.

1. Request with missing query field

request:

curl -XPOST 'http://localhost:8080/graphql' -H 'content-type: application/json' --data-raw '{}'

throws:

java.lang.RuntimeException: Query can not be null
	at io.smallrye.graphql.execution.ExecutionService.execute(ExecutionService.java:122)
	at io.smallrye.graphql.execution.ExecutionService.executeAsync(ExecutionService.java:104)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.doRequest(SmallRyeGraphQLExecutionHandler.java:305)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.handlePost(SmallRyeGraphQLExecutionHandler.java:121)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.doHandle(SmallRyeGraphQLExecutionHandler.java:82)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handleWithIdentity(SmallRyeGraphQLAbstractHandler.java:95)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handle(SmallRyeGraphQLAbstractHandler.java:76)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handle(SmallRyeGraphQLAbstractHandler.java:30)
	at io.vertx.ext.web.impl.BlockingHandlerDecorator.lambda$handle$0(BlockingHandlerDecorator.java:48)
	at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
	at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
	at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
	at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:833)

2. Request with broken JSON

request:

curl -XPOST 'http://localhost:8080/graphql' -H 'content-type: application/json' --data-raw '{'`

throws:

javax.json.stream.JsonParsingException: Invalid token=EOF at (line no=1, column no=3, offset=2). Expected tokens are: [STRING, CURLYCLOSE]
	at org.glassfish.json.JsonParserImpl.parsingException(JsonParserImpl.java:426)
	at org.glassfish.json.JsonParserImpl.access$1100(JsonParserImpl.java:55)
	at org.glassfish.json.JsonParserImpl$ObjectContext.getNextEvent(JsonParserImpl.java:446)
	at org.glassfish.json.JsonParserImpl.hasNext(JsonParserImpl.java:341)
	at org.glassfish.json.JsonParserImpl.getObject(JsonParserImpl.java:310)
	at org.glassfish.json.JsonParserImpl.getObject(JsonParserImpl.java:149)
	at org.glassfish.json.JsonReaderImpl.readObject(JsonReaderImpl.java:88)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.getJsonObjectFromBody(SmallRyeGraphQLExecutionHandler.java:197)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.handlePost(SmallRyeGraphQLExecutionHandler.java:101)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLExecutionHandler.doHandle(SmallRyeGraphQLExecutionHandler.java:82)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handleWithIdentity(SmallRyeGraphQLAbstractHandler.java:95)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handle(SmallRyeGraphQLAbstractHandler.java:76)
	at io.quarkus.smallrye.graphql.runtime.SmallRyeGraphQLAbstractHandler.handle(SmallRyeGraphQLAbstractHandler.java:30)
	at io.vertx.ext.web.impl.BlockingHandlerDecorator.lambda$handle$0(BlockingHandlerDecorator.java:48)
	at io.vertx.core.impl.ContextBase.lambda$null$0(ContextBase.java:137)
	at io.vertx.core.impl.ContextInternal.dispatch(ContextInternal.java:264)
	at io.vertx.core.impl.ContextBase.lambda$executeBlocking$1(ContextBase.java:135)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
	at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.base/java.lang.Thread.run(Thread.java:833)
@jmartisk
Copy link
Member

jmartisk commented Mar 30, 2023

We've recently improved handling of invalid requests via #1666
The actual PR #1719 - it returns an ExecutionResult with a GraphQLError saying Unparseable input document

This should be solved in SmallRye GraphQL 2.1.0+, which will be part of Quarkus 3.0. Is this solution sufficient for you?

@ikenox
Copy link
Author

ikenox commented Mar 30, 2023

The two cases I described seems to be different from the issue that has been solved the PR #1719.
My point was that those cases should be also handled in the same way as for unparseable input document error.

As I described as curl command above,

  • In the latest code 279372f , request with JSON body {} throws RuntimeError at here.
  • And request with JSON body { throws JsonParsingException. But sorry, that was not smallrye-graphql problem but quarkus-smallrye-graphql)

@jmartisk
Copy link
Member

@ikenox , oh yes you're right, these are a bit different. I'll prepare some fixes, these will be needed both on smallrye and on quarkus side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants