diff --git a/lib/jekyll/command.rb b/lib/jekyll/command.rb index 2df86ed3..50cfe8de 100644 --- a/lib/jekyll/command.rb +++ b/lib/jekyll/command.rb @@ -73,6 +73,30 @@ module Jekyll "Fail if errors are present in front matter" end # rubocop:enable Metrics/MethodLength + + # Run ::process method in a given set of Jekyll::Command subclasses and suggest + # re-running the associated command with --trace switch to obtain any additional + # information or backtrace regarding the encountered Exception. + # + # cmd - the Jekyll::Command to be handled + # options - configuration overrides + # klass - an array of Jekyll::Command subclasses associated with the command + # + # Note that all exceptions are rescued.. + # rubocop: disable RescueException + def process_with_graceful_fail(cmd, options, *klass) + klass.each { |k| k.process(options) if k.respond_to?(:process) } + rescue Exception => e + raise e if cmd.trace + + msg = " Please append `--trace` to the `#{cmd.name}` command " + dashes = "-" * msg.length + Jekyll.logger.error "", dashes + Jekyll.logger.error "Jekyll #{Jekyll::VERSION} ", msg + Jekyll.logger.error "", " for any additional information or backtrace. " + Jekyll.logger.abort_with "", dashes + end + # rubocop: enable RescueException end end end diff --git a/lib/jekyll/commands/build.rb b/lib/jekyll/commands/build.rb index 96a48cdf..a5b26704 100644 --- a/lib/jekyll/commands/build.rb +++ b/lib/jekyll/commands/build.rb @@ -15,7 +15,7 @@ module Jekyll c.action do |_, options| options["serving"] = false - Jekyll::Commands::Build.process(options) + process_with_graceful_fail(c, options, self) end end end diff --git a/lib/jekyll/commands/serve.rb b/lib/jekyll/commands/serve.rb index 7efeb72d..efdaea67 100644 --- a/lib/jekyll/commands/serve.rb +++ b/lib/jekyll/commands/serve.rb @@ -72,29 +72,21 @@ module Jekyll opts["serving"] = true opts["watch"] = true unless opts.key?("watch") - start(opts) + # Set the reactor to nil so any old reactor will be GCed. + # We can't unregister a hook so while running tests we don't want to + # inadvertently keep using a reactor created by a previous test. + @reload_reactor = nil + + config = configuration_from_options(opts) + config["url"] = default_url(config) if Jekyll.env == "development" + + process_with_graceful_fail(cmd, config, Build, Serve) end end end # - def start(opts) - # Set the reactor to nil so any old reactor will be GCed. - # We can't unregister a hook so in testing when Serve.start is - # called multiple times we don't want to inadvertently keep using - # a reactor created by a previous test when our test might not - @reload_reactor = nil - - config = configuration_from_options(opts) - if Jekyll.env == "development" - config["url"] = default_url(config) - end - [Build, Serve].each { |klass| klass.process(config) } - end - - # - def process(opts) opts = configuration_from_options(opts) destination = opts["destination"]