Shopify/liquid · error · Liquid::ZeroDivisionError
e.message (re-raised as Liquid::ZeroDivisionError from…
Error message
e.message (re-raised as Liquid::ZeroDivisionError from ::ZeroDivisionError in divided_by)
What it means
The `divided_by` filter wraps Ruby's native ::ZeroDivisionError and re-raises it as Liquid::ZeroDivisionError. This happens when the divisor operand evaluates to zero (or 0.0 for float division producing Infinity handling in some cases). Liquid converts the low-level Ruby error so templates surface a consistent error type.
Solutions
- Guard the divisor in the template: use `{% if divisor != 0 %}` before applying divided_by.
- Default the operand with the `default` filter: `{{ total | divided_by: count | default: 0 }}` only helps if count is nil, so validate count != 0 first.
- Fix the data source so the denominator is never 0 (e.g. skip averaging when items list is empty).
- Rescue Liquid::ZeroDivisionError in your render error handling and render a fallback message.
- Ensure operand is coerced correctly (strings like "0" also trigger it; check the value bound to the variable).
Example fix
// before
{% assign avg = total | divided_by: count %}
// after
{% if count > 0 %}{% assign avg = total | divided_by: count %}{% else %}{% assign avg = 0 %}{% endif %} Defensive patterns
Strategy: try-catch
Validate before calling
{% if divisor and divisor != 0 %}{{ total | divided_by: divisor }}{% else %}0{% endif %} Type guard
def zero?(v) v.respond_to?(:to_f) && v.to_f == 0.0 end
Try / catch
begin template.render(assigns) rescue Liquid::ZeroDivisionError => e fallback_division_output end
Prevention
- Always validate denominators are non-zero before binding them to template assigns
- Use the `default` filter for nil operands but still check for literal 0
- Add render-level error handling for Liquid::ZeroDivisionError in production
- Unit-test templates with zero/empty denominator edge cases
When it happens
Trigger: Calling a template filter like `{{ 10 | divided_by: 0 }}` or `{{ price | divided_by: divisor }}` where `divisor` is 0 or evaluates to 0 after Utils.to_number coercion.
Common situations: Rendering templates with computed divisors (counts, averages, percentages) where the denominator variable is empty, 0, or nil-coerced to 0; e.g. average-per-item calculations over empty collections.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- e.message (re-raised as Liquid::ZeroDivisionError from…
- e.message (re-raised as Liquid::FloatDomainError from…
- e.message (re-raised as Liquid::FloatDomainError from…
- e.message (re-raised as Liquid::FloatDomainError from…
- undefined filter #
AI-assisted analysis of Shopify/liquid@807d45a6b3 (2026-09-08).
Data as JSON: /api/errors/81c4e4ba055f47ba.
Report an issue: GitHub.
Appendix: source
Thrown at lib/liquid/standardfilters.rb:866
# @liquid_summary
# Multiplies a number by a given number.
# @liquid_syntax number | times: number
# @liquid_return [number]
def times(input, operand)
apply_operation(input, operand, :*)
end
# @liquid_public_docs
# @liquid_type filter
# @liquid_category math
# @liquid_summary
# Divides a number by a given number. The `divided_by` filter produces a result of the same type as the divisor. This means if you divide by an integer, the result will be an integer, and if you divide by a float, the result will be a float.
# @liquid_syntax number | divided_by: number
# @liquid_return [number]
def divided_by(input, operand)
apply_operation(input, operand, :/)
rescue ::ZeroDivisionError => e
raise Liquid::ZeroDivisionError, e.message
end
# @liquid_public_docs
# @liquid_type filter
# @liquid_category math
# @liquid_summary
# Returns the remainder of dividing a number by a given number.
# @liquid_syntax number | modulo: number
# @liquid_return [number]
def modulo(input, operand)
apply_operation(input, operand, :%)
rescue ::ZeroDivisionError => e
raise Liquid::ZeroDivisionError, e.message
end
# @liquid_public_docs
# @liquid_type filter
# @liquid_category mathView on GitHub (pinned to 807d45a6b3)