How to properly indicate an error during site generation?


#1

Maybe I am missing the obvious, but I don’t understand how to stop site generation if I detect malformed posts. What I would like to do is to add an assert-like construct. I would like to use it for example in an include file to check for the existence of a parameter that is used within the include or in a layout file to make sure that the front matter of a post contains a specific property. Whenever the assertion fails, I would like to “throw an exception” to indicate the problem and express the solution in a descriptive way (e.g., “include X requires parameter Y, but you did not specify it”).

I think I know how to check for the existence of the parameter, but I don’t know how to stop the generation (or at least produce a warning)… I tried searching for the obvious candidates (stop/exit/throw/fail/etc.), but all I could find were countless posts, in which users had trouble getting their Jekyll installation to run. :confused:

Do you have any idea how to solve this in a non-ruby-plugin (i.e., GitHub pages compatible) way?


#2

I’m surprised this doesn’t have an answer yet! I’ve got the same need.

One workaround is through writing a kludgy plugin for a Liquid filter that simply takes an error message and raises an exception:

module Jekyll
  module ExceptionFilter
    def raise_error(msg)
    	bad_file = @context.registers[:page]['path']
    	err_msg = "On #{bad_file}: #{msg}"
      raise err_msg
    end
  end
end

Liquid::Template.register_filter(Jekyll::ExceptionFilter)

Then, within a template, for example, I can include this:

{% unless page.necessary_field %}
    {{ "You're missing necessary_field" | raise_error }}
{% endunless %}

This sort of gets the job done. One problem I have is that the build process halts immediately on finding one error. I would like to collect a list of the errors across all pages to display at the end. I thought that liquid: error_mode: warn in _config.yml would do this, but it does not.