Skip to content

Commit d6c7421

Browse files
committed
Use custom error instead of ArgumentError
Using ArgumentError sounds like a good idea since we received invalid argument to this method, but this exception is suppose to be when you pass the wrong number of arguments or missing keywords.
1 parent fe71dd1 commit d6c7421

File tree

6 files changed

+28
-14
lines changed

6 files changed

+28
-14
lines changed

lib/css_parser.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
require 'css_parser/parser'
1616

1717
module CssParser
18+
class Error < StandardError; end
19+
class EmptyValueError < Error; end
20+
1821
# Merge multiple CSS RuleSets by cascading according to the CSS 2.1 cascading rules
1922
# (http://www.w3.org/TR/REC-CSS2/cascade.html#cascading-order).
2023
#

lib/css_parser/parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def add_rule!(*args, selectors: nil, block: nil, filename: nil, offset: nil, med
199199
)
200200

201201
add_rule_set!(rule_set, media_types)
202-
rescue ArgumentError => e
202+
rescue CssParser::Error => e
203203
raise e if @options[:rule_set_exceptions]
204204
end
205205
end

lib/css_parser/rule_set/declarations.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def value=(value)
1616
value = value.to_s.sub(/\s*;\s*\Z/, '')
1717
self.important = !value.slice!(CssParser::IMPORTANT_IN_PROPERTY_RX).nil?
1818
value.strip!
19-
raise ArgumentError, 'value is empty' if value.empty?
19+
raise EmptyValueError, 'value is empty' if value.empty?
2020

2121
@value = value.freeze
2222
end
@@ -67,10 +67,12 @@ def []=(property, value)
6767
elsif value.to_s.strip.empty?
6868
delete property
6969
else
70-
declarations[property] = Value.new(value)
70+
begin
71+
declarations[property] = Value.new(value)
72+
rescue EmptyValueError => e
73+
raise e.exception, "#{property} #{e.message}"
74+
end
7175
end
72-
rescue ArgumentError => e
73-
raise e.exception, "#{property} #{e.message}"
7476
end
7577
alias add_declaration! []=
7678

test/rule_set/declarations/test_value.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ class RuleSetProperyTest < Minitest::Test
88
describe '.new' do
99
describe 'with invalid value' do
1010
it 'raises an error when empty' do
11-
exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new(' ') }
11+
exception = assert_raises(CssParser::EmptyValueError) { CssParser::RuleSet::Declarations::Value.new(' ') }
1212
assert_equal 'value is empty', exception.message
1313
end
1414

1515
it 'raises an error when nil' do
16-
exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new(nil) }
16+
exception = assert_raises(CssParser::EmptyValueError) { CssParser::RuleSet::Declarations::Value.new(nil) }
1717
assert_equal 'value is empty', exception.message
1818
end
1919

2020
it 'raises an error when contains only important declaration' do
21-
exception = assert_raises(ArgumentError) { CssParser::RuleSet::Declarations::Value.new(' !important; ') }
21+
exception = assert_raises(CssParser::EmptyValueError) { CssParser::RuleSet::Declarations::Value.new(' !important; ') }
2222
assert_equal 'value is empty', exception.message
2323
end
2424
end
@@ -103,7 +103,13 @@ class RuleSetProperyTest < Minitest::Test
103103
assert_equal true, CssParser::RuleSet::Declarations::Value.new('value').value.frozen?
104104
end
105105

106-
it 'raises an exception when the value is empty' do
106+
it 'raises an EmptyValueError when an empty string is passed' do
107+
assert_raises CssParser::EmptyValueError do
108+
CssParser::RuleSet::Declarations::Value.new ""
109+
end
110+
end
111+
112+
it 'raises an ArgumentError when no argument is supplied' do
107113
assert_raises ArgumentError do
108114
CssParser::RuleSet::Declarations::Value.new
109115
end

test/rule_set/test_declarations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ class RuleSetDeclarationsTest < Minitest::Test
7373
it 'raises an exception including the property when the value is empty' do
7474
declarations = CssParser::RuleSet::Declarations.new
7575

76-
assert_raises ArgumentError do
76+
assert_raises CssParser::EmptyValueError do
7777
declarations['foo'] = '!important'
78-
rescue ArgumentError => e
78+
rescue CssParser::EmptyValueError => e
7979
assert_equal e.message, 'foo value is empty'
8080
raise e
8181
end

test/test_css_parser_misc.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ def test_enumerator_nonempty
229229

230230
def test_catching_argument_exceptions_for_add_rule
231231
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
232-
assert_raises ArgumentError, 'background-color value is empty' do
232+
error = assert_raises CssParser::EmptyValueError do
233233
cp_with_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important')
234234
end
235+
assert_equal error.message, 'background-color value is empty'
235236

236237
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
237238
cp_without_exceptions.add_rule!(selectors: 'body', block: 'background-color: !important')
@@ -240,12 +241,13 @@ def test_catching_argument_exceptions_for_add_rule
240241
def test_catching_argument_exceptions_for_add_rule_positional
241242
cp_with_exceptions = Parser.new(rule_set_exceptions: true)
242243

243-
assert_raises ArgumentError, 'background-color value is empty' do
244+
error = assert_raises CssParser::EmptyValueError do
244245
_, err = capture_io do
245246
cp_with_exceptions.add_rule!('body', 'background-color: !important')
246247
end
247248
assert_includes err, "DEPRECATION"
248249
end
250+
assert_equal error.message, 'background-color value is empty'
249251

250252
cp_without_exceptions = Parser.new(rule_set_exceptions: false)
251253
_, err = capture_io do
@@ -257,12 +259,13 @@ def test_catching_argument_exceptions_for_add_rule_positional
257259
def test_catching_argument_exceptions_for_add_rule_with_offsets
258260
cp_with_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: true)
259261

260-
assert_raises ArgumentError, 'background-color value is empty' do
262+
error = assert_raises CssParser::EmptyValueError do
261263
_, err = capture_io do
262264
cp_with_exceptions.add_rule_with_offsets!('body', 'background-color: !important', 'inline', 1)
263265
end
264266
assert_includes err, "DEPRECATION"
265267
end
268+
assert_equal error.message, 'background-color value is empty'
266269

267270
cp_without_exceptions = Parser.new(capture_offsets: true, rule_set_exceptions: false)
268271
_, err = capture_io do

0 commit comments

Comments
 (0)