Error Handling

Troubleshooting

When using the Web Action SDK, you may encounter various errors. This guide will help you understand and handle these errors effectively.

Common Errors and How to Handle Them

BadParametersError

This error occurs when the input parameters for an action are invalid.

Function: Any SDK action
 
Error handling:
try {
  // Your SDK action call
} catch (error) {
  if (error instanceof BadParametersError) {
    console.error("Invalid parameters:", error.message);
    // Handle the error, e.g., prompt the user to correct their input
  }
}

UnauthorizedError

This error is thrown when there's an authentication issue with the platform account.

Function: Any SDK action
 
Error handling:
try {
  // Your SDK action call
} catch (error) {
  if (error instanceof UnauthorizedError) {
    console.error("Authentication error:", error.message);
    // Handle the error, e.g., prompt the user to log in again
  }
}

IntegrationTimeoutError

This error occurs when an integration operation times out.

Function: Any SDK action
 
Error handling:
try {
  // Your SDK action call
} catch (error) {
  if (error instanceof IntegrationTimeoutError) {
    console.error("Operation timed out:", error.message);
    // Handle the error, e.g., retry the operation or inform the user
  }
}

InternalServerError

This error is thrown when there's an unexpected error on the server side.

Function: Any SDK action
 
Error handling:
try {
  // Your SDK action call
} catch (error) {
  if (error instanceof InternalServerError) {
    console.error("Server error:", error.message);
    // Handle the error, e.g., inform the user and log the error
  }
}

Timeout Error

While not a specific error type, timeouts can occur due to network issues or slow responses.

Function: Any SDK action
 
Error handling:
const timeoutPromise = new Promise((_, reject) =>
  setTimeout(() => reject(new Error("Operation timed out")), 30000)
);
 
try {
  const result = await Promise.race([
    yourSdkAction(),
    timeoutPromise
  ]);
  // Process the result
} catch (error) {
  if (error.message === "Operation timed out") {
    console.error("The operation timed out");
    // Handle the timeout, e.g., retry or inform the user
  } else {
    // Handle other errors
  }
}

General Error Handling Strategy

It's a good practice to implement a general error handling strategy that can catch and process all types of errors:

try {
  // Your SDK action call
} catch (error) {
  if (error instanceof BadParametersError) {
    console.error("Invalid parameters:", error.message);
    // Handle invalid parameters
  } else if (error instanceof UnauthorizedError) {
    console.error("Authentication error:", error.message);
    // Handle authentication issues
  } else if (error instanceof IntegrationTimeoutError) {
    console.error("Operation timed out:", error.message);
    // Handle timeouts
  } else if (error instanceof InternalServerError) {
    console.error("Server error:", error.message);
    // Handle server errors
  } else {
    console.error("An unexpected error occurred:", error.message);
    // Handle any other unexpected errors
  }
}

Logging and Monitoring

To help with troubleshooting, it's recommended to implement logging in your application. This can help you identify patterns in errors and improve your error handling over time.

import { logger } from './your-logging-library';
 
try {
  // Your SDK action call
} catch (error) {
  logger.error('Error in SDK action', {
    error: error.message,
    stack: error.stack,
    action: 'yourSdkAction',
    params: yourParams
  });
  // Handle the error
}

Retrying Failed Operations

For some errors, especially those related to network issues or timeouts, it might be appropriate to retry the operation:

import { retry } from './your-retry-library';
 
const retryOptions = {
  retries: 3,
  factor: 2,
  minTimeout: 1000,
  maxTimeout: 5000
};
 
try {
  const result = await retry(async () => {
    // Your SDK action call
  }, retryOptions);
  // Process the result
} catch (error) {
  console.error("Operation failed after retries:", error.message);
  // Handle the error
}

Remember to adjust your error handling strategy based on the specific needs of your application and the expectations of your users.