Errors
Errors can be caught and interrogated by wrapping the operation in a try catch block:
1
async function createComment(input: LinearDocument.CommentCreateInput): LinearFetch<Comment | UserError> {
2
try {
3
/** Try to create a comment */
4
const commentPayload = await linearClient.commentCreate(input);
5
/** Return it if available */
6
return commentPayload.comment;
7
} catch (error) {
8
/** The error has been parsed by Linear Client */
9
throw error;
10
}
11
}
Copied!
Or by catching the error thrown from a calling function:
1
async function archiveFirstIssue(): LinearFetch<ArchivePayload> {
2
const me = await linearClient.viewer;
3
const issues = await me.assignedIssues();
4
const firstIssue = issues.nodes[0];
5
6
if (firstIssue?.id) {
7
const payload = await linearClient.issueArchive(firstIssue.id);
8
return payload;
9
} else {
10
return undefined;
11
}
12
}
13
14
archiveFirstIssue().catch(error => {
15
throw error;
16
});
Copied!
The parsed error type can be compared to determine the course of action:
1
import { InvalidInputLinearError, LinearError, LinearErrorType } from '@linear/sdk'
2
import { UserError } from './custom-errors'
3
4
const input = { name: "Happy Team" };
5
createTeam(input).catch(error => {
6
if (error instanceof InvalidInputLinearError) {
7
/** If the mutation has failed due to an invalid user input return a custom user error */
8
return new UserError(input, error);
9
} else {
10
/** Otherwise throw the error and handle in the calling function */
11
throw error;
12
}
13
});
Copied!
Information about the request resulting in the error is attached if available:
1
run().catch(error => {
2
if (error instanceof LinearError) {
3
console.error("Failed query:", error.query);
4
console.error("With variables:", error.variables);
5
}
6
throw error;
7
});
Copied!
Information about the response is attached if available:
1
run().catch(error => {
2
if (error instanceof LinearError) {
3
console.error("Failed HTTP status:", error.status);
4
console.error("Failed response data:", error.data);
5
}
6
throw error;
7
});
Copied!
Any GraphQL errors are parsed and added to an array:
1
run().catch(error => {
2
if (error instanceof LinearError) {
3
error.errors?.map(graphqlError => {
4
console.log("Error message", graphqlError.message);
5
console.log("LinearErrorType of this GraphQL error", graphqlError.type);
6
console.log("Error due to user input", graphqlError.userError);
7
console.log("Path through the GraphQL schema", graphqlError.path);
8
});
9
}
10
throw error;
11
});
Copied!
The raw error returned by the LinearGraphQLClient is still available:
1
run().catch(error => {
2
if (error instanceof LinearError) {
3
console.log("The original error", error.raw);
4
}
5
throw error;
6
});
Copied!
Copy link