Handling Errors

When interacting with the Firmhouse API, you might encounter errors such as validation errors, not found errors, server errors etc. You can handle these errors by catching them and handling them accordingly.

Not Found Error

import { NotFoundError } from '@firmhouse/firmhouse-sdk';

try {
  const products = await client.products.fetch("invalid-product-id")
} catch (error) {
  if (error instanceof NotFoundError) {
   console.log("Product not found");
  }
}

Validation Error

import { ValidationError } from '@firmhouse/firmhouse-sdk';

try {
  const cart = await client.carts.updateAddressDetails("<cart-token>", { email: "invalid-email" });
} catch (error) {
 if (error instanceof ValidationError) {
   const { details } = error;
   const { email } = details;
   console.log(email); // "Email is not valid"
 }
}

Server Error

import { ServerError } from '@firmhouse/firmhouse-sdk';

try {
 const subscription = await client.subscriptions.get("invalid-subscription-token");
} catch (error) {
  if (error instanceof ServerError) {
    console.log(error.message);
  }
}

Last updated