Errors can be caught and interrogated by wrapping the operation in a try catch block:
asyncfunctioncreateComment(input:LinearDocument.CommentCreateInput):LinearFetch<Comment|UserError> {try {/** Try to create a comment */constcommentPayload=awaitlinearClient.createComment(input);/** Return it if available */returncommentPayload.comment; } catch (error) {/** The error has been parsed by Linear Client */throw error; }}
Or by catching the error thrown from a calling function:
The parsed error type can be compared to determine the course of action:
import { InvalidInputLinearError, LinearError, LinearErrorType } from '@linear/sdk'
import { UserError } from './custom-errors'
const input = { name: "Happy Team" };
createTeam(input).catch(error => {
if (error instanceof InvalidInputLinearError) {
/** If the mutation has failed due to an invalid user input return a custom user error */
return new UserError(input, error);
} else {
/** Otherwise throw the error and handle in the calling function */
throw error;
}
});
Information about the request resulting in the error is attached if available:
Any GraphQL errors are parsed and added to an array:
run().catch(error => {
if (error instanceof LinearError) {
error.errors?.map(graphqlError => {
console.log("Error message", graphqlError.message);
console.log("LinearErrorType of this GraphQL error", graphqlError.type);
console.log("Error due to user input", graphqlError.userError);
console.log("Path through the GraphQL schema", graphqlError.path);
});
}
throw error;
});
The raw error returned by the LinearGraphQLClient is still available:
run().catch(error => {
if (error instanceof LinearError) {
console.log("The original error", error.raw);
}
throw error;
});