Skip to content

Commit b26a668

Browse files
committed
Show the package name for which the error associated
In certain cases, it's unclear which package causes an error during shard installation. These changes improves error messages by adding explicit package context. When an error occurs, the log will now indicate which package triggered the failure.
1 parent e74e050 commit b26a668

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

spec/integration/install_spec.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ describe "install" do
855855
with_shard(metadata) do
856856
ex = expect_raises(FailedCommand) { run "shards install --no-color" }
857857
ex.stdout.should contain <<-ERROR
858-
E: Could not find executable "nonexistent"
858+
E: Failed to install `executable_missing`: Could not find executable "nonexistent"
859859
ERROR
860860
end
861861
end

src/cli.cr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,19 @@ end
176176
begin
177177
Shards.run
178178
rescue ex : OptionParser::InvalidOption
179-
Shards::Log.fatal { ex.message }
179+
Shards::Log.fatal(exception: ex) { ex.message }
180+
Shards::Log.trace { ex.inspect_with_backtrace }
180181
exit 1
181182
rescue ex : Shards::ParseError
182183
ex.to_s(STDERR)
183184
exit 1
185+
rescue ex : Shards::Package::Error
186+
package = ex.package
187+
Shards::Log.error(exception: ex) { "Failed to install `#{package.name}`: #{ex.message}" }
188+
Shards::Log.trace { ex.inspect_with_backtrace }
189+
exit 1
184190
rescue ex : Shards::Error
185-
Shards::Log.error { ex.message }
191+
Shards::Log.error(exception: ex) { ex.message }
192+
Shards::Log.trace { ex.inspect_with_backtrace }
186193
exit 1
187194
end

src/package.cr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ require "./helpers"
33

44
module Shards
55
class Package
6+
class Error < Shards::Error
7+
getter package
8+
9+
def initialize(message, @package : Package)
10+
super message
11+
end
12+
end
13+
614
getter name : String
715
getter resolver : Resolver
816
getter version : Version
@@ -114,7 +122,7 @@ module Shards
114122
spec.executables.each do |name|
115123
exe_name = find_executable_file(Path[install_path], name)
116124
unless exe_name
117-
raise Shards::Error.new("Could not find executable #{name.inspect}")
125+
raise Shards::Package::Error.new("Could not find executable #{name.inspect}", package: self)
118126
end
119127
Log.debug { "Install #{exe_name}" }
120128
source = File.join(install_path, exe_name)

0 commit comments

Comments
 (0)