Skip to content

Commit eebada2

Browse files
authored
Merge pull request #32349 from jmartisk/main-srgql-1795
Better error on unparseable GraphQL JSON request
2 parents e889bbd + cd07bdb commit eebada2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java

+20
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
import jakarta.json.JsonObject;
1313
import jakarta.json.JsonObjectBuilder;
1414
import jakarta.json.JsonReader;
15+
import jakarta.json.stream.JsonParsingException;
1516

1617
import org.jboss.logging.Logger;
1718

1819
import graphql.ErrorType;
1920
import graphql.ExecutionResult;
21+
import graphql.ExecutionResultImpl;
2022
import graphql.GraphQLError;
23+
import graphql.GraphqlErrorBuilder;
2124
import graphql.execution.AbortExecutionException;
2225
import io.quarkus.security.identity.CurrentIdentityAssociation;
2326
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
@@ -122,6 +125,8 @@ private void handlePost(HttpServerResponse response, RoutingContext ctx, String
122125
}
123126
} catch (IOException ex) {
124127
throw new RuntimeException(ex);
128+
} catch (JsonParsingException ex) {
129+
sendError("Unparseable request", response, ctx, requestedCharset);
125130
}
126131
}
127132

@@ -299,6 +304,21 @@ private String getAllowedMethods() {
299304
}
300305
}
301306

307+
private void sendError(String errorMessage, HttpServerResponse response,
308+
RoutingContext ctx, String requestedCharset) {
309+
VertxExecutionResponseWriter writer = new VertxExecutionResponseWriter(response, ctx, requestedCharset);
310+
GraphQLError error = GraphqlErrorBuilder
311+
.newError()
312+
.message(errorMessage)
313+
.build();
314+
ExecutionResult executionResult = ExecutionResultImpl
315+
.newExecutionResult()
316+
.addError(error)
317+
.build();
318+
ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
319+
writer.write(executionResponse);
320+
}
321+
302322
private void doRequest(JsonObject jsonInput, HttpServerResponse response, RoutingContext ctx,
303323
String requestedCharset) {
304324
VertxExecutionResponseWriter writer = new VertxExecutionResponseWriter(response, ctx, requestedCharset);

0 commit comments

Comments
 (0)