Skip to content

Commit c749d01

Browse files
committed
Add helpful error message for gem loading issues in hooks
Previously, if you had a `require` statement in a hook for a gem that wasn't installed or wasn't specified in the loaded Gemfile, you would get an unhelpful load error. Provide some helpful guidance in this case since it's almost always the problem when it comes to load errors, and people will likely forget to add a gem to their custom `.overcommit_gems.rb` file if they have one.
1 parent 7f14be6 commit c749d01

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/overcommit/configuration.rb

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def ==(other)
1818
end
1919
alias_method :eql?, :==
2020

21+
# Access the configuration as if it were a hash.
22+
#
23+
# @param key [String]
24+
# @return [Array,Hash,Number,String]
25+
def [](key)
26+
@hash[key]
27+
end
28+
2129
# Returns absolute path to the directory that external hook plugins should
2230
# be loaded from.
2331
def plugin_directory

lib/overcommit/hook_runner.rb

+12
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ def load_hooks
144144

145145
# Load plugin hooks after so they can subclass existing hooks
146146
@hooks += HookLoader::PluginHookLoader.new(@config, @context, @log).load_hooks
147+
rescue LoadError => ex
148+
# Include a more helpful message that will probably save some confusion
149+
message = 'A load error occurred. ' +
150+
if @config['gemfile']
151+
"Did you forget to specify a gem in your `#{@config['gemfile']}`?"
152+
else
153+
'Did you forget to install a gem?'
154+
end
155+
156+
raise Overcommit::Exceptions::HookLoadError,
157+
"#{message}\n#{ex.message}",
158+
ex.backtrace
147159
end
148160
end
149161
end

template-dir/hooks/overcommit-hook

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ rescue Overcommit::Exceptions::HookContextLoadError => error
7575
puts error
7676
puts 'Are you running an old version of Overcommit?'
7777
exit 69 # EX_UNAVAILABLE
78+
rescue Overcommit::Exceptions::HookLoadError => error
79+
puts error.message
80+
puts error.backtrace
81+
exit 78 # EX_CONFIG
7882
rescue Overcommit::Exceptions::HookSetupFailed,
7983
Overcommit::Exceptions::HookCleanupFailed => error
8084
puts error.message

0 commit comments

Comments
 (0)