Skip to content

Commit ef8dc82

Browse files
authored
Test invalid importmap file (#101)
* Raise custom error when we fail to parse importmap Raising a custom error means it's a bit easier to track across error monitoring tools. * Rescue StandardError when parsing importmap files We usually don't want to rescue the base Exception class as these errors are reserved for things like a system interrupt. This commit swaps that out for rescuing a StandardErrorr.
1 parent 247b35a commit ef8dc82

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/importmap/map.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
class Importmap::Map
44
attr_reader :packages, :directories
55

6+
class InvalidFile < StandardError; end
7+
68
def initialize
79
@packages, @directories = {}, {}
810
end
@@ -11,9 +13,9 @@ def draw(path = nil, &block)
1113
if path && File.exist?(path)
1214
begin
1315
instance_eval(File.read(path), path.to_s)
14-
rescue Exception => e
16+
rescue StandardError => e
1517
Rails.logger.error "Unable to parse import map from #{path}: #{e.message}"
16-
raise "Unable to parse import map from #{path}: #{e.message}"
18+
raise InvalidFile, "Unable to parse import map from #{path}: #{e.message}"
1719
end
1820
elsif block_given?
1921
instance_eval(&block)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
typo "react", to: "https://ga.jspm.io/npm:[email protected]/index.js"

test/importmap_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def setup
5656
assert_match %r|assets/my_lib-.*\.js|, generate_importmap_json["imports"]["my_lib"]
5757
end
5858

59+
test 'invalid importmap file results in error' do
60+
file = file_fixture('invalid_import_map.rb')
61+
importmap = Importmap::Map.new
62+
assert_raises Importmap::Map::InvalidFile do
63+
importmap.draw(file)
64+
end
65+
end
66+
5967
test "preloaded modules are included in preload tags" do
6068
preloading_module_paths = @importmap.preloaded_module_paths(resolver: ApplicationController.helpers).to_s
6169
assert_match /md5/, preloading_module_paths

0 commit comments

Comments
 (0)