Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise missing asset error in more places #16

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/propshaft/compilers/css_asset_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def asset_url(resolved_path)
if asset = assembly.load_path.find(resolved_path)
%[url("#{assembly.config.prefix}/#{asset.digested_path}")]
else
raise Propshaft::MissingAssetError, "The asset '#{resolved_path}' was not found in the load path."
raise Propshaft::MissingAssetError.new(resolved_path)
end
end
end
11 changes: 10 additions & 1 deletion lib/propshaft/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@ module Propshaft
class Error < StandardError; end

# Raised when LoadPath cannot find the requested asset
class MissingAssetError < Error; end
class MissingAssetError < Error
def initialize(path)
super
@path = path
end

def message
"The asset '#{@path}' was not found in the load path."
end
end
end
2 changes: 2 additions & 0 deletions lib/propshaft/resolver/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(load_path:, prefix:)
def resolve(logical_path)
if asset = load_path.find(logical_path)
File.join prefix, asset.digested_path
else
raise Propshaft::MissingAssetError.new(logical_path)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/propshaft/resolver/static.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(manifest_path:, prefix:)
def resolve(logical_path)
if asset_path = parsed_manifest[logical_path]
File.join prefix, asset_path
else
raise Propshaft::MissingAssetError.new(logical_path)
end
end

Expand Down
6 changes: 4 additions & 2 deletions test/propshaft/resolver/dynamic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class Propshaft::Resolver::DynamicTest < ActiveSupport::TestCase
@resolver.resolve("one.txt")
end

test "resolving missing asset returns nil" do
assert_nil @resolver.resolve("nowhere.txt")
test "resolving missing asset raises a custom error" do
assert_raise Propshaft::MissingAssetError do
assert_nil @resolver.resolve("nowhere.txt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assert_nil here is misleading and unnecessary after the method was changed to raise an exception:

Suggested change
assert_nil @resolver.resolve("nowhere.txt")
@resolver.resolve("nowhere.txt")

However, rather than making that change, I've proposed a small refactor in #20: feec9cf

end
end
end
6 changes: 4 additions & 2 deletions test/propshaft/resolver/static_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class Propshaft::Resolver::StaticTest < ActiveSupport::TestCase
@resolver.resolve("one.txt")
end

test "resolving missing asset returns nil" do
assert_nil @resolver.resolve("nowhere.txt")
test "resolving missing asset raises a custom error" do
assert_raise Propshaft::MissingAssetError do
assert_nil @resolver.resolve("nowhere.txt")
end
end
end