How to handle component not found error?

I have a cart transformation with merge and expand operation. When an item is missing, I get a function error:
[
“operations[0] (expand): [{code: component_merchandise_not_found, message: One or more of the expanded cart items have a merchandise id that does not exist.}]”
]

From the list of errors, it says:
One or more of the expanded cart items have a merchandise id that does not exist.

What can I do in my function? Let’s say a merchant removes a variant from the store. The variant is not available. I don’t understand why it’s an error? Isn’t it normal if a variant is discontinued from selling?

This is indeed a common scenario when variants are discontinued. Your function should be resilient to these changes by implementing proper error handling.

You can implement validation logic to verify merchandise IDs exist before expansion and create fallback behavior when variants are discontinued. For example, you could:

  • Skip the missing item
  • Replace with an alternative
  • Show a helpful error message

I add the parent variant to the cart for this expand operation. In the js code, I can get the component variant IDs from the metafield. How can I check the variant availability with the variant IDs?

If you have the ID, I believe you can use the productVariant query. The ProductVariant object then has an availableForSale field, as well as an inventoryQuantity.

Hope that helps!

Shopify functions don’t support the graphql admin API: About the Cart Transform Function API

Apologies, you are right, this is not supported. In that case it looks like you’ll have to handle the component_merchandise_not_found error gracefully in your function.

How can I handle the error gracefully if I cannot check the variants availability in the function js? After I return the function result, Shopify returns the error.

Your doc only lists the error codes but not the way to handle them: About the Cart Transform Function API

How you handle the error is up to you. You can display a user-friendly error message, for example.