Best Practices

Follow these best practices for robust error handling in your application.

Always Use Try-Catch

Wrap SDK calls in try-catch blocks:

try {
  const vault = await sdk.vault.getVaultByKey("yusd", 1);
} catch (error) {
  // Handle error
}

Check Error Types

Use instanceof to check error types:

import {
  AuthenticationError,
  NetworkError,
  ValidationError,
} from "yieldfi-sdk";

try {
  await sdk.auth.login(credentials);
} catch (error) {
  if (error instanceof AuthenticationError) {
    // Handle auth errors
  } else if (error instanceof NetworkError) {
    // Handle network errors
  } else if (error instanceof ValidationError) {
    // Handle validation errors
  }
}

Provide User-Friendly Messages

Translate technical errors to user-friendly messages:

Implement Retry Logic

For network errors, implement retry logic:

Handle Token Expiration

Always handle token expiration gracefully:

Log Errors Appropriately

Log errors for debugging but don't expose sensitive information:

Handle Loading States

Show loading states during API calls:

Graceful Degradation

Handle partial failures gracefully:

Last updated