Shopify themes, liquid, logos, and UX
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!
Solved! Go to the solution
This is an accepted solution.
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.
@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?
This is an accepted solution.
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.
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.
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025