Skip to content

Commit

Permalink
Merge pull request #32349 from jmartisk/main-srgql-1795
Browse files Browse the repository at this point in the history
Better error on unparseable GraphQL JSON request
  • Loading branch information
geoand authored Apr 3, 2023
2 parents e889bbd + cd07bdb commit eebada2
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonReader;
import jakarta.json.stream.JsonParsingException;

import org.jboss.logging.Logger;

import graphql.ErrorType;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.execution.AbortExecutionException;
import io.quarkus.security.identity.CurrentIdentityAssociation;
import io.quarkus.vertx.http.runtime.CurrentVertxRequest;
Expand Down Expand Up @@ -122,6 +125,8 @@ private void handlePost(HttpServerResponse response, RoutingContext ctx, String
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} catch (JsonParsingException ex) {
sendError("Unparseable request", response, ctx, requestedCharset);
}
}

Expand Down Expand Up @@ -299,6 +304,21 @@ private String getAllowedMethods() {
}
}

private void sendError(String errorMessage, HttpServerResponse response,
RoutingContext ctx, String requestedCharset) {
VertxExecutionResponseWriter writer = new VertxExecutionResponseWriter(response, ctx, requestedCharset);
GraphQLError error = GraphqlErrorBuilder
.newError()
.message(errorMessage)
.build();
ExecutionResult executionResult = ExecutionResultImpl
.newExecutionResult()
.addError(error)
.build();
ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
writer.write(executionResponse);
}

private void doRequest(JsonObject jsonInput, HttpServerResponse response, RoutingContext ctx,
String requestedCharset) {
VertxExecutionResponseWriter writer = new VertxExecutionResponseWriter(response, ctx, requestedCharset);
Expand Down

0 comments on commit eebada2

Please sign in to comment.