How to handle errors

Master error handling with the Picsart TypeScript SDK by learning how to manage specific error classes like ForbiddenError and ValidationError. This guide provides practical examples to help you gracefully handle errors and provide informative feedback for troubleshooting. The advantages of client-side validation are highlighted, showing how early error detection can save processing time, enhance security, and improve both developer and end-user experience. Unlock the full potential of the SDK to build more resilient applications.

Before you start

Check these out before you start

Handle Errors

In addition to handling transient errors, the API provides specific error classes to help you manage different types of errors more effectively. Here's an example demonstrating how to handle both ForbiddenError and ValidationError:

import PicsartEnterprise, { AdjustRequest, ImageApi } from "../src";
import { ForbiddenError, ValidationError } from "../src/core/errors";

(async () => {
  const imageApi = PicsartEnterprise.createImageApi(process.env.PICSART_API_KEY!);
  const imageSource = ImageApi.fromUrl(process.env.IMAGE_URL!);

  try {
    const result = await imageApi.adjust(
      new AdjustRequest()
        .setImage(imageSource)
        .setBrightness(22222222222222) // !!!Invalid brightness value
    );

    console.log(result.image.url);
  } catch (err) {
    if (err instanceof ForbiddenError) {
      // Handle ForbiddenError
      console.log("Access denied:", err.message);
      console.log("Details:", err.detail);
      return;
    }

    if (err instanceof ValidationError) {
      // Handle ValidationError
      console.log("Validation error:", err.message);
      console.log("Details:", err.detail);
      return;
    }

    // Rethrow any other errors
    throw err;
  }
})();

In this example, an attempt is made to set the brightness to an excessively high value 22222222222222. When the API detects the invalid value, it throws a ValidationError, which is handled in the catch block. If a ForbiddenError occurs, it logs the appropriate messages. This approach allows developers to manage specific error cases gracefully and provides informative feedback for troubleshooting.

Benefits of Client-Side Validation with the TypeScript SDK

Using the TypeScript SDK offers significant benefits, primarily due to its client-side validation capabilities. These validations are performed before making any HTTP requests, which drastically reduces processing time and improves both developer and end-user experience. By catching errors early, the SDK ensures that only valid requests are sent to the server, leading to faster development cycles and more efficient error handling. This proactive approach not only saves bandwidth and server resources but also provides immediate feedback, helping developers quickly identify and resolve issues. Additionally, this client-side validation enhances security by preventing potentially harmful requests from reaching the server.