Best Practices

Error Handling

Proper error handling is crucial for managing network issues and API responses effectively. Here's how to implement robust error handling in your ExaDrive SDK usage:

Implementing Try-Catch Blocks

Always wrap SDK operations in try-catch blocks to handle potential errors:

async function uploadFile(filePath) {
  try {
    const result = await exaDrive.uploadFile(filePath);
    console.log('File uploaded successfully:', result);
    return result;
  } catch (error) {
    console.error('Error uploading file:', error.message);
    // Implement appropriate error handling logic
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.error('Server responded with error:', error.response.data);
    } else if (error.request) {
      // The request was made but no response was received
      console.error('No response received from server');
    } else {
      // Something happened in setting up the request that triggered an Error
      console.error('Error setting up request:', error.message);
    }
    throw error; // Re-throw or handle as needed
  }
}

Custom Error Handling

Create custom error handling functions to manage specific types of errors:

function handleApiError(error) {
  if (error.response) {
    switch (error.response.status) {
      case 401:
        console.error('Authentication failed. Please check your APP_ID and API_KEY.');
        // Implement logic to refresh authentication or prompt for new credentials
        break;
      case 403:
        console.error('Access forbidden. Your account may not have the necessary permissions.');
        break;
      case 429:
        console.error('Rate limit exceeded. Please try again later.');
        // Implement retry logic with exponential backoff
        break;
      default:
        console.error('An unexpected error occurred:', error.response.data);
    }
  } else {
    console.error('Network error:', error.message);
  }
}

Asynchronous Operations for Large File Transfers

For large file transfers, use asynchronous operations to keep your application responsive:

Implementing Progress Tracking

async function uploadLargeFile(filePath) {
  const fileSize = await getFileSize(filePath); // Implement this function
  let uploadedBytes = 0;

  try {
    await exaDrive.uploadFile(filePath, {
      onUploadProgress: (progressEvent) => {
        uploadedBytes = progressEvent.loaded;
        const percentCompleted = Math.round((uploadedBytes * 100) / fileSize);
        console.log(`Upload progress: ${percentCompleted}%`);
        // Update UI with progress
      }
    });
    console.log('Large file upload completed successfully');
  } catch (error) {
    console.error('Error uploading large file:', error);
    handleApiError(error);
  }
}

Regular SDK Updates

Staying up-to-date with the latest SDK version is crucial for security and feature improvements:

Automated Update Checking

Implement a function to check for SDK updates:

const axios = require('axios');
const semver = require('semver');
const currentVersion = require('./package.json').dependencies['exadrive-sdk'];

async function checkForUpdates() {
  try {
    const response = await axios.get('https://registry.npmjs.org/exadrive-sdk');
    const latestVersion = response.data['dist-tags'].latest;
    
    if (semver.gt(latestVersion, currentVersion)) {
      console.log(`A new version of ExaDrive SDK is available: ${latestVersion}`);
      console.log('Run "npm update exadrive-sdk" to update.');
    } else {
      console.log('ExaDrive SDK is up to date.');
    }
  } catch (error) {
    console.error('Failed to check for updates:', error);
  }
}

// Run this check periodically or at application startup
checkForUpdates();

Troubleshooting Guide

When encountering issues with the ExaDrive SDK, follow these steps:

  1. Verify SDK Version: Ensure you're using the latest version of the SDK:

    bashnpm list exadrive-sdk

    If outdated, update:

    bashnpm update exadrive-sdk
  2. Validate Credentials: Double-check your APP_ID and API_KEY:

    async function validateCredentials() {
      try {
        await exaDrive.validateCredentials(); // Implement this method in your SDK
        console.log('Credentials are valid');
      } catch (error) {
        console.error('Invalid credentials:', error);
      }
    }
  3. Network Connectivity Check: Implement a network check function:

    async function checkNetworkConnectivity() {
      try {
        await axios.get('https://api.exadrive.com/health'); // Replace with actual health check endpoint
        console.log('Network connection to ExaDrive API is successful');
      } catch (error) {
        console.error('Network connectivity issue:', error);
      }
    }
  4. Logging and Debugging: Implement comprehensive logging:

    const winston = require('winston');
    const logger = winston.createLogger({
      level: 'info',
      format: winston.format.json(),
      defaultMeta: { service: 'exadrive-sdk' },
      transports: [
        new winston.transports.File({ filename: 'error.log', level: 'error' }),
        new winston.transports.File({ filename: 'combined.log' })
      ]
    });
    
    // Use logger instead of console.log/error in your application

By implementing these practices and following the troubleshooting guide, you can ensure a smooth integration of the ExaDrive SDK into your application, handle errors effectively, and maintain an up-to-date and efficient implementation.

Last updated