Handle OAuth2 and API errors
masterThe client uses custom error types to allow for programmatic error handling. All errors are thrown and never returned as successful responses.
Error Types:
OAuthError: Base class for all OAuth and API errors. Includeserrorcode,description, andintuit_tid(Transaction ID).TokenError: For token-related issues (expired, invalid, etc.).ValidationError: For input validation errors.NetworkError: For network connectivity issues.
Handling OAuth2 specific errors (e.g., from createToken() or refresh()):
You can inspect the error property for specific OAuth2 error strings like invalid_grant or invalid_client.
try {
const authResponse = await oauthClient.createToken(code);
const token = authResponse.getToken();
} catch (error) {
// Access detailed OAuth2 error information
console.error('OAuth Error:', error.error); // e.g., "invalid_grant"
console.error('Description:', error.error_description);
console.error('Transaction ID:', error.intuit_tid);
console.error('Full response:', error.authResponse.json);
if (error.error === 'invalid_grant') {
// Handle expired/invalid authorization code
}
}