Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
GraphQL client - Handle error response
The graphql:Client
allows handling different errors occurred when executing the execute
method. It returns a graphql:ClientError
error, which has different subtypes that can be handled differently based on the use case. Use the subtypes of the graphql:ClientError
to handle different types of errors based on the use case.
import ballerina/graphql;
import ballerina/io;
type ProfileResponse record {|
*graphql:GenericResponseWithErrors;
record {|Profile profile;|} data;
|};
type Profile record {|
string name;
int age;
|};
public function main() returns error? {
do {
graphql:Client graphqlClient = check new ("localhost:9090/graphql");
// This is a malformed GraphQL document.
string document = "mutation { updateName(name: 1) { name, age } }";
ProfileResponse response = check graphqlClient->execute(document);
io:println(response);
} on fail graphql:ClientError err {
handleErrors(err);
}
}
function handleErrors(graphql:ClientError clientError) {
if clientError is graphql:PayloadBindingError {
// This error represents a client-side data binding error. This error occurs due to the
// assigned variable type and the value obtained from the wire having a mismatching
// shape.
io:println("PayloadBindingError: ", clientError.message());
} else if clientError is graphql:InvalidDocumentError {
// This error represents GraphQL errors due to GraphQL server-side document
// validation. The GraphQL errors returned from the server-side can be obtained by
// calling the `detail` method on the `graphql:InvalidDocumentError`.
graphql:ErrorDetail[]? errorDetails = clientError.detail().errors;
io:println("InvalidDocumentError: ", errorDetails);
} else if clientError is graphql:HttpError {
// This error represents network-level errors. If the response from the server contains
// a body then, it can be obtained by calling the `detail` method on the `graphql:HttpError`.
anydata body = clientError.detail().body;
io:println("HttpError: ", body, clientError.message());
}
}
Prerequisites
- • Run the GraphQL service given in the Mutations example.
Run the client program by executing the following command.
$ bal run graphql_client_error_handling.balInvalidDocumentError: [{"message":"String cannot represent non String value: 1","locations":[{"line":1,"column":29}]},{"message":"Field "updateName" argument "id" of type "Int!" is required, but it was not provided.","locations":[{"line":1,"column":12}]}]
Related links
PreviousHandle partial response
NextSSL/TLS