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

Fix 4xx error handling #195

Merged
merged 11 commits into from
Apr 24, 2021

Conversation

abhishek-12355
Copy link
Contributor

Fixes #194

@codecov-io
Copy link

codecov-io commented Feb 19, 2021

Codecov Report

Merging #195 (870be27) into master (459d5eb) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #195   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           14        16    +2     
  Lines          949       984   +35     
=========================================
+ Hits           949       984   +35     
Impacted Files Coverage Δ
gql/transport/aiohttp.py 100.00% <100.00%> (ø)
gql/transport/exceptions.py 100.00% <100.00%> (ø)
gql/transport/requests.py 100.00% <100.00%> (ø)
gql/utils.py 100.00% <0.00%> (ø)
gql/__init__.py 100.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 459d5eb...870be27. Read the comment docs.

@abhishek-12355
Copy link
Contributor Author

I've tried fixing the Lint issues. When I run them locally, they don't complain of anything, but on github they are throwing errors.
@syrusakbary @BossGrand @jkimbo

@leszekhanusz
Copy link
Collaborator

Regarding the linting issues, you probably don't have the correct dev dependencies.
Did you check the CONTRIBUTING.md file ?
Once you have the correct dependencies, the linting can be done with make check

@abhishek-12355
Copy link
Contributor Author

@leszekhanusz Thanks. It is fixed now.

@codecov-commenter
Copy link

codecov-commenter commented Apr 24, 2021

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (c4f2dc2) to head (0e9b657).
Report is 154 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #195   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           16        16           
  Lines          978       985    +7     
=========================================
+ Hits           978       985    +7     

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

@leszekhanusz
Copy link
Collaborator

I made a refactor in order to avoid checking for the content_type. Could you please check that it is ok for you ?

@leszekhanusz
Copy link
Collaborator

To resume, here was the situation previously:

JSON:
    data or errors keys:
        ExecutionResult
    no data or errors keys:
        TransportProtocolError
Not JSON:
    status < 400:
        TransportProtocolError
    status >= 400:
        TransportServerError

Now we have:

JSON:
    data or errors keys:
        ExecutionResult
    no data or errors keys:
        status < 400:
            TransportProtocolError
        status >= 400:
            TransportServerError        
Not JSON:
    status < 400:
        TransportProtocolError
    status >= 400:
        TransportServerError

Note that we still set an ExecutionResult if we receive a correct json with errors keys even if the http code is higher than 400.

@abhishek-12355
Copy link
Contributor Author

I made a refactor in order to avoid checking for the content_type. Could you please check that it is ok for you ?

Looks good to me.

@yassinsameh
Copy link

How to get error info following a 400 error response. I get only a bas request line vs Details when using any Api platform.

For example, running this query in Insomnia or Postman i get the useful error message:
"GraphQL.Execution.UnhandledError: Error trying to resolve field 'transactions'.\n ---> System.NullReferenceException: Object reference not set to an instance of an object.\n at GraphQL.Execution.ExecutionStrategy.<CollectFieldsFrom>g__GetFragmentSpreads|9_3(ExecutionContext context, GraphQLSelectionSet selectionSet) in /_/src/GraphQL/Execution/ExecutionStrategy.cs:line 340\n at GraphQL.Execution.ExecutionStrategy.CollectFieldsFrom(ExecutionContext context, IGraphType specificType, GraphQLSelectionSet selectionSet, Dictionary 2 fields) in /_/src/GraphQL/Execution/ExecutionStrategy.cs:line 257\n at GraphQL.Execution.ExecutionStrategy.SetSubFieldNodes(ExecutionContext context, ObjectExecutionNode parent) in /_/src/GraphQL/Execution/ExecutionStrategy.cs:line 163\n at GraphQL.Execution.ExecutionStrategy.CompleteNodeAsync(ExecutionContext context, ExecutionNode node) in /_/src/GraphQL/Execution/ExecutionStrategy.cs:line 579\n --- End of inner exception stack trace ---"

Using the gql client:

def graph_ql_query(url, query,operation_name,variables, bearer,site_id,response_key):
    try:
        headers = {'Content-Type': 'application/json','X-SITE-ID': site_id,'Authorization':bearer}
        transport = RequestsHTTPTransport(url=url,headers=headers,
                                                    retries=2,
                                                    verify=True
                                                        )
        # Create a GraphQL client using the defined transport
        client = Client(transport=transport, fetch_schema_from_transport=False)
        # Execute the query on the transport
        response =  client.execute(query,variable_values=variables,operation_name=operation_name)
        
        return response.get(response_key)
    except TransportServerError as e:
        # Handle GraphQL query errors
        KLogger.logger.error(f"Failed to execute query on transport: {e}")
        raise ValidationError("Failed to get your payments profile")

    except Exception as e:
        KLogger.logger.error(f"Failed to execute query: {e}")
        raise ValidationError("Failed to get your payments profile")

The only output i get and stacktrace:

Traceback (most recent call last):
   File "/py/lib/python3.10/site-packages/gql/transport/requests.py", line 231, in raise_response_error
     resp.raise_for_status()
   File "/py/lib/python3.10/site-packages/requests/models.py", line 1021, in raise_for_status
     raise HTTPError(http_error_msg, response=self)
 requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.site.io/core/graphql
 
 The above exception was the direct cause of the following exception:
 

 Traceback (most recent call last):
   File "/app/payments/utils.py", line 29, in graph_ql_query
     response =  client.execute(query,variable_values=variables,operation_name=operation_name)
   File "/py/lib/python3.10/site-packages/gql/client.py", line 403, in execute
     return self.execute_sync(
   File "/py/lib/python3.10/site-packages/gql/client.py", line 221, in execute_sync
     return session.execute(
   File "/py/lib/python3.10/site-packages/gql/client.py", line 849, in execute
     result = self._execute(
   File "/py/lib/python3.10/site-packages/gql/client.py", line 758, in _execute
     result = self.transport.execute(
   File "/py/lib/python3.10/site-packages/gql/transport/requests.py", line 252, in execute
     raise_response_error(response, 'No "data" or "errors" keys in answer')
   File "/py/lib/python3.10/site-packages/gql/transport/requests.py", line 233, in raise_response_error
     raise TransportServerError(str(e), e.response.status_code) from e
 gql.transport.exceptions.TransportServerError: 400 Client Error: Bad Request for url: https://api.site.io/core/graphql

Am i missing something? Thank you @leszekhanusz

@leszekhanusz
Copy link
Collaborator

@yassinsameh Please open another issue when you have a problem instead of adding a comment to a closed PR.

Concerning your problem, gql error handling is explained here.

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

Successfully merging this pull request may close these issues.

4XX errors with valid json not being properly handled
5 participants