diff --git a/lib/sass/script_error.rb b/lib/sass/script_error.rb index 68932aa4..924434d3 100644 --- a/lib/sass/script_error.rb +++ b/lib/sass/script_error.rb @@ -2,5 +2,9 @@ module Sass # An exception thrown by Sass Script. - class ScriptError < StandardError; end + class ScriptError < StandardError + def initialize(message, name = nil) + super(name.nil? ? message : "$#{name}: #{message}") + end + end end diff --git a/lib/sass/value.rb b/lib/sass/value.rb index 5a632bb0..0507926f 100644 --- a/lib/sass/value.rb +++ b/lib/sass/value.rb @@ -57,52 +57,52 @@ def to_nil # @return [Boolean] # @raise [ScriptError] def assert_boolean(name = nil) - raise error("#{self} is not a boolean", name) + raise Sass::ScriptError.new("#{self} is not a boolean", name) end # @raise [ScriptError] def assert_calculation(name = nil) - raise error("#{self} is not a calculation", name) + raise Sass::ScriptError.new("#{self} is not a calculation", name) end # @return [Color] # @raise [ScriptError] def assert_color(name = nil) - raise error("#{self} is not a color", name) + raise Sass::ScriptError.new("#{self} is not a color", name) end # @return [Function] # @raise [ScriptError] def assert_function(name = nil) - raise error("#{self} is not a function", name) + raise Sass::ScriptError.new("#{self} is not a function", name) end # @return [Map] # @raise [ScriptError] def assert_map(name = nil) - raise error("#{self} is not a map", name) + raise Sass::ScriptError.new("#{self} is not a map", name) end # @return [Number] # @raise [ScriptError] def assert_number(name = nil) - raise error("#{self} is not a number", name) + raise Sass::ScriptError.new("#{self} is not a number", name) end # @return [String] # @raise [ScriptError] def assert_string(name = nil) - raise error("#{self} is not a string", name) + raise Sass::ScriptError.new("#{self} is not a string", name) end # @param sass_index [Number] # @return [Integer] def sass_index_to_array_index(sass_index, name = nil) index = sass_index.assert_number(name).assert_integer(name) - raise error('List index may not be 0', name) if index.zero? + raise Sass::ScriptError.new('List index may not be 0', name) if index.zero? if index.abs > to_a_length - raise error("Invalid index #{sass_index} for a list with #{to_a_length} elements", name) + raise Sass::ScriptError.new("Invalid index #{sass_index} for a list with #{to_a_length} elements", name) end index.negative? ? to_a_length + index : index - 1 @@ -113,10 +113,6 @@ def sass_index_to_array_index(sass_index, name = nil) def to_a_length 1 end - - def error(message, name = nil) - Sass::ScriptError.new name.nil? ? message : "$#{name}: #{message}" - end end end diff --git a/lib/sass/value/color.rb b/lib/sass/value/color.rb index 0210a3ec..48eaab6f 100644 --- a/lib/sass/value/color.rb +++ b/lib/sass/value/color.rb @@ -44,7 +44,7 @@ def initialize(red: nil, hwb_to_rgb @whiteness = @blackness = nil else - raise error 'Invalid Color' + raise Sass::ScriptError, 'Invalid Color' end end diff --git a/lib/sass/value/list.rb b/lib/sass/value/list.rb index e3c817d1..cb9802c9 100644 --- a/lib/sass/value/list.rb +++ b/lib/sass/value/list.rb @@ -13,7 +13,7 @@ class List # @param bracketed [::Boolean] def initialize(contents = [], separator: ',', bracketed: false) if separator.nil? && contents.length > 1 - raise error 'A list with more than one element must have an explicit separator' + raise Sass::ScriptError, 'A list with more than one element must have an explicit separator' end @contents = contents.freeze diff --git a/lib/sass/value/number.rb b/lib/sass/value/number.rb index eb9f68bc..48276923 100644 --- a/lib/sass/value/number.rb +++ b/lib/sass/value/number.rb @@ -24,12 +24,16 @@ def initialize(value, unit = nil) denominator_units = [] when ::Hash numerator_units = unit.fetch(:numerator_units, []) - raise error "invalid numerator_units #{numerator_units.inspect}" unless numerator_units.is_a? Array + unless numerator_units.is_a? Array + raise Sass::ScriptError, "invalid numerator_units #{numerator_units.inspect}" + end denominator_units = unit.fetch(:denominator_units, []) - raise error "invalid denominator_units #{denominator_units.inspect}" unless denominator_units.is_a? Array + unless denominator_units.is_a? Array + raise Sass::ScriptError, "invalid denominator_units #{denominator_units.inspect}" + end else - raise error "invalid unit #{unit.inspect}" + raise Sass::ScriptError, "invalid unit #{unit.inspect}" end unless denominator_units.empty? && numerator_units.empty? @@ -105,7 +109,7 @@ def unitless? # @return [Number] # @raise [ScriptError] def assert_unitless(name = nil) - raise error "Expected #{self} to have no units", name unless unitless? + raise Sass::ScriptError.new "Expected #{self} to have no units", name unless unitless? self end @@ -125,7 +129,7 @@ def unit?(unit) # @return [Number] # @raise [ScriptError] def assert_unit(unit, name = nil) - raise error "Expected #{self} to have unit #{unit.inspect}", name unless unit?(unit) + raise Sass::ScriptError.new "Expected #{self} to have unit #{unit.inspect}", name unless unit?(unit) self end @@ -138,7 +142,7 @@ def integer? # @return [Integer] # @raise [ScriptError] def assert_integer(name = nil) - raise error "#{self} is not an integer", name unless integer? + raise Sass::ScriptError.new "#{self} is not an integer", name unless integer? to_i end @@ -271,7 +275,8 @@ def coerce_or_convert_value(new_numerator_units, new_denominator_units, other: nil, other_name: nil) if other && (other.numerator_units != new_denominator_units && other.denominator_units != new_denominator_units) - raise error "Expect #{other} to have units #{unit_string(new_numerator_units, new_denominator_units).inspect}" + raise Sass::ScriptError, "Expect #{other} to have units #{unit_string(new_numerator_units, + new_denominator_units).inspect}" end return value if numerator_units == new_numerator_units && denominator_units == new_denominator_units diff --git a/lib/sass/value/string.rb b/lib/sass/value/string.rb index 1c3c6847..d1eb9907 100644 --- a/lib/sass/value/string.rb +++ b/lib/sass/value/string.rb @@ -42,10 +42,10 @@ def assert_string(_name = nil) # @return [Integer] def sass_index_to_string_index(sass_index, name = nil) index = sass_index.assert_number(name).assert_integer(name) - raise error('String index may not be 0', name) if index.zero? + raise Sass::ScriptError.new('String index may not be 0', name) if index.zero? if index.abs > text.length - raise error("Invalid index #{sass_index} for a string with #{text.length} characters", name) + raise Sass::ScriptError.new("Invalid index #{sass_index} for a string with #{text.length} characters", name) end index.negative? ? text.length + index : index - 1