How can I resolve a very infrequent Liquid error when trying to access collection.products?

Topic summary

A developer is troubleshooting an intermittent “Liquid error (sections/main-collection line 898): internal” that occurs when loading paginated product collections in Shopify. The error appears infrequently—sometimes going weeks without occurring—making it difficult to diagnose.

Initial attempts:

  • Checking collection.products.first before looping
  • Using collection.products.size > 0 condition
  • Both approaches still produced the error

Key insights from community:

  • The error predates recent code changes and occurred even with the previous theme
  • Checking for collection.products.first before the loop may be problematic
  • Using {% unless collection.empty? %} is more reliable than {% if collection %} since empty objects evaluate as truthy in Liquid

Solution implemented:

  • Wrap the code in a {% capture %} block to catch errors
  • Use {% if output contains "Liquid error (" %} to detect when errors occur
  • Display a graceful error message instead of showing the raw Liquid error
  • Utilize the {% else %} clause within the for loop for empty collections

The issue is now handled gracefully with proper error detection, though the root cause of the intermittent “internal” error remains unclear.

Summarized with AI on October 29. AI used: claude-sonnet-4-5-20250929.

Hello all,

I have been running into an internal error when the Shopify website I’m working on attempts to load a paginated list of store products. The error is usually very rare but recently has shown up more frequently (could go weeks without showing up once):

Liquid error (sections/main-collection line 898): internal

The error is not very clear and I have no clue if “internal,” is in reference to my code, or something to do with Shopify.

In any case, here is one attempt at the code which was working fairly decently until one day this error started showing up infrequently:

{% if collection %}
{% if collection.products.first %} // → this line throws the error
{%- for product in collection.products -%}
{%- render ‘product-item’, product: product, list: show_as_list, grid_classes: grid_classes -%}
{%- endfor -%}
{% else %}
Some easy to understand error message to user
{% endif %}
{% endif %}

I have also tried {% if collection and collection.products.size > 0 %} but still no luck.

If it helps, 48 products are loaded per page. I kind of think it may be loading too many products at once, but 50 is the default so I’m not sure.

If anyone knows what might be the issue and either a possible solution, or even a way to handle the error gracefully, I would greatly appreciate the help!

@dev_nigel if this error started to show suddenly, then is it due to any theme or code update or app addition you did recently?

Actually this has been an issue even before I started working on the site.
Apparently it even happened with the old theme, but it would just show an empty page.

I’ve been trying to do as much error checking as necessary to minimize the problem, but it happens so rarely that I can’t really determine the nature of the error. Worst of all is that I can’t really log out the collections object to see what’s wrong with it (undefined, null, etc.)

@dev_nigel - is it because you are checking first product and then putting for loop , can you try by putting if condition inside for loop?

Thanks I’ll give it a shot and report back, but what makes you think this might be the right approach?

In your code you do not need the second if line – this should work fine:

{% if collection %}
  {%- for product in collection.products -%}
    {%- render 'product-item', product: product, list: show_as_list, grid_classes: grid_classes -%}
  {% else %}
    Some easy to understand error message to user
  {%- endfor -%}
{% endif %}

https://shopify.dev/docs/api/liquid/tags/iteration-else

The way to handle the error is to use capture:

{% capture output %}
  {% if collection %}
    {%- for product in collection.products -%}
      {%- render 'product-item', product: product, list: show_as_list, grid_classes: grid_classes -%}
    {% else %}
      Some easy to understand error message to user
    {%- endfor -%}
  {% endif %}
{% endcapture %}

{% assign output = output | strip %}
{% if output == "internal" %}
  error handling here
{%  else %}
  {{ output }}
{% endif %}

Also, I do not know where are you’re getting your collection variable from, but if, say collection is a string, then {% if collection %} would still be true, but collection.products will generate error. Even empty object is true!
https://shopify.dev/docs/api/liquid/basics#truthy-and-falsy

You should try {% unless collection.empty? %} instead –
https://shopify.dev/docs/api/liquid/basics#empty

An empty object is returned if you try to access an object that is defined, but has no value. For example a page or product that’s been deleted, or a setting with no value.> > You can compare an object with empty to check whether an object exists before you access any of its attributes.

1 Like

Thanks for the through breakdown Tim, this might be the right avenue to take.

I did still encounter the same error but now for this line:

{%- for product in collection.products -%}

Maybe I should use this to try and detect the error?

{% if output contains "Liquid error (" %}

Update:

capturing was the right call and using

{% if output contains “Liquid error (” %}

was enough for me to detect the error and at least display an error message.

Outside of this edit to the code, Tim’s suggestion is the correct solution.