diff --git a/CHANGELOG.md b/CHANGELOG.md index ed343bf..ad5612b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). User.parse(id: "123") # ✓ valid (name is optional) ``` +### Changed + +- Standardize error messages to use lowercase for consistency with Ruby conventions +- Improve type error messages to include attribute name for easier debugging + ## [4.1.0] - 2025-10-09 ### Added diff --git a/lib/structure/builder.rb b/lib/structure/builder.rb index 59f7786..b94209c 100644 --- a/lib/structure/builder.rb +++ b/lib/structure/builder.rb @@ -46,7 +46,7 @@ def attribute(name, type = nil, from: nil, default: nil, null: true, &block) @non_nullable.add(name) unless null if type && block - raise ArgumentError, "Cannot specify both type and block for :#{name}" + raise ArgumentError, "cannot specify both type and block for :#{name}" else types[name] = type || block end @@ -102,7 +102,12 @@ def non_nullable = @non_nullable.to_a # @api private def coercions(context = nil) - @types.transform_values { |type| Types.coerce(type, context) } + @types.to_h do |attr, type| + coercion = Types.coerce(type, context) + [attr, coercion] + rescue ArgumentError => e + raise ArgumentError, "#{e.message} for :#{attr}" + end end # @api private diff --git a/lib/structure/types.rb b/lib/structure/types.rb index 99dde7e..2667572 100644 --- a/lib/structure/types.rb +++ b/lib/structure/types.rb @@ -60,7 +60,7 @@ def coerce(type, context = nil) when String lazy_class(type, context) else - raise ArgumentError, "Cannot specify #{type.inspect} as type" + raise ArgumentError, "cannot specify #{type.inspect} as type" end end diff --git a/test/test_core_structure.rb b/test/test_core_structure.rb index bc9d028..db0d2a7 100644 --- a/test/test_core_structure.rb +++ b/test/test_core_structure.rb @@ -85,7 +85,7 @@ def test_attribute_with_both_type_and_block_raises_error end end - assert_match(/Cannot specify both type and block/, error.message) + assert_match(/cannot specify both type and block/, error.message) end def test_attribute_with_default_value diff --git a/test/test_type_coercions.rb b/test/test_type_coercions.rb index 4421f6d..cbbb88b 100644 --- a/test/test_type_coercions.rb +++ b/test/test_type_coercions.rb @@ -152,10 +152,12 @@ def test_empty_hash_type_raises_error def test_invalid_object_type_raises_error invalid_object = Object.new - assert_raises(ArgumentError) do + error = assert_raises(ArgumentError) do Structure.new do attribute(:value, invalid_object) end end + + assert_match(/cannot specify.*as type for :value/, error.message) end end