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

Map GraphQL UnknowOperationException as bad request #5335

Conversation

vpaturet
Copy link
Contributor

Summary

When receiving a malformed GraphQL query that refers to an unexisting operation:

{"query":"{\n trip(\n from: {place: \"NSR:StopPlace:58189\"}\n to: {place: \"NSR:StopPlace:58287\"}\n numTripPatterns: 5\n ) {\n tripPatterns {\n duration\n legs {\n expectedStartTime\n expectedEndTime\n mode\n distance\n line {\n id\n publicCode\n }\n fromEstimatedCall {\n destinationDisplay {\n frontText\n }\n }\n fromPlace {\n name\n quay {\n publicCode\n }\n }\n toPlace {\n name\n quay {\n publicCode\n }\n }\n }\n }\n }\n}\n","variables":null,"operationName":"j"}

OTP logs an error message:

Unknown operation named 'j'.
graphql.execution.UnknownOperationException: Unknown operation named 'j'.
	at graphql.language.NodeUtil.getOperation(NodeUtil.java:84)
	at graphql.execution.Execution.execute(Execution.java:59)
	at graphql.GraphQL.execute(GraphQL.java:565)
	at graphql.GraphQL.lambda$parseValidateAndExecute$12(GraphQL.java:484)
	at java.base/java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1187)
	at java.base/java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2309)
	at graphql.GraphQL.parseValidateAndExecute(GraphQL.java:479)
	at graphql.GraphQL.executeAsync(GraphQL.java:438)
	at graphql.GraphQL.execute(GraphQL.java:365)
	at org.opentripplanner.ext.transmodelapi.TransmodelGraph.executeGraphQL(TransmodelGraph.java:65)
	at org.opentripplanner.ext.transmodelapi.TransmodelAPI.getGraphQL(TransmodelAPI.java:122)

and returns a GraphQL response with HTTP code 500:

{
    "errors": [
        {
            "message": "Unknown operation named 'j'.",
            "locations": [],
            "extensions": {
                "classification": "InternalServerError"
            }
        }
    ]
}

This PR ensures that the API returns an HTTP code 400 - Bad Request with the following GraphQL response:

{
    "errors": [
        {
            "message": "Unknown operation named 'j'.",
            "locations": [],
            "extensions": {
                "classification": "BadRequestError"
            }
        }
    ]
}

Issue

No

Unit tests

Documentation

No

Changelog

No

@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Patch coverage has no change and project coverage change: +0.01% 🎉

Comparison is base (36edab0) 66.38% compared to head (1d762a6) 66.39%.

Additional details and impacted files
@@              Coverage Diff              @@
##             dev-2.x    #5335      +/-   ##
=============================================
+ Coverage      66.38%   66.39%   +0.01%     
- Complexity     15176    15179       +3     
=============================================
  Files           1785     1785              
  Lines          69163    69163              
  Branches        7334     7334              
=============================================
+ Hits           45914    45922       +8     
+ Misses         20767    20758       -9     
- Partials        2482     2483       +1     
Files Changed Coverage Δ
...tripplanner/ext/transmodelapi/TransmodelGraph.java 0.00% <0.00%> (ø)

... and 4 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@vpaturet vpaturet marked this pull request as ready for review August 30, 2023 11:55
@vpaturet vpaturet requested a review from a team as a code owner August 30, 2023 11:55
@leonardehrenfried leonardehrenfried added this to the 2.4 (next release) milestone Aug 31, 2023
@leonardehrenfried leonardehrenfried merged commit 09a16e2 into opentripplanner:dev-2.x Aug 31, 2023
5 checks passed
@vpaturet vpaturet deleted the map_graphql_unknown_operation_error branch October 16, 2023 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants