Handle GraphQL errors vs GraphErrors
mainWhen working with GraphClient, you must distinguish between two types of errors:
1. GraphQL Errors (Internal Query Errors)
These are returned inside the GraphResponse object. They represent issues with the GraphQL query itself (e.g., a field doesn't exist).
- Note:
errorsanddataare not mutually exclusive; a response can contain both. - Usage: These are for debugging purposes only and should not be shown to end-users.
- Check for them using
response.hasErrors()and retrieve details viaresponse.formatErrorMessage().
2. GraphError (Execution/Network Errors)
These represent critical failures in executing or processing the request and are delivered via the onFailure callback.
Common GraphError types include:
GraphCallCanceledError: The call was canceled.GraphHttpError: The HTTP response status code was not in the 200 series.GraphNetworkError: Network issues like timeouts or being offline.GraphParseError: Failed to parse the JSON response.GraphError: An unknown reason prevented execution.
// Handling GraphError in onFailure
@Override public void onFailure(GraphError error) {
if (error instanceof GraphCallCanceledError) {
Log.e(TAG, "Call has been canceled", error);
} else if (error instanceof GraphHttpError) {
Log.e(TAG, "Http request failed: " + ((GraphHttpError) error).formatMessage(), error);
} else if (error instanceof GraphNetworkError) {
Log.e(TAG, "Network is not available", error);
} else if (error instanceof GraphParseError) {
Log.e(TAG, "Failed to parse GraphQL response", error);
} else {
Log.e(TAG, "Failed to execute GraphQL request", error);
}
}