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

Solved

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

dev_nigel
Shopify Partner
5 0 0

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!

Accepted Solution (1)

tim
Shopify Partner
4505 536 1642

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.



 

If my post is helpful, hit the thumb up button -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com

View solution in original post

Replies 7 (7)

suyash1
Shopify Partner
10982 1359 1735

@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?

To build shopify pages use PAGEFLY | Contact me - suyash.patankar@gmail.com , My timezone is GMT+5:30.
dev_nigel
Shopify Partner
5 0 0

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.)

suyash1
Shopify Partner
10982 1359 1735

@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?

To build shopify pages use PAGEFLY | Contact me - suyash.patankar@gmail.com , My timezone is GMT+5:30.
dev_nigel
Shopify Partner
5 0 0

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

tim
Shopify Partner
4505 536 1642

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.



 

If my post is helpful, hit the thumb up button -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
dev_nigel
Shopify Partner
5 0 0

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 (" %}

 

dev_nigel
Shopify Partner
5 0 0

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.