Improve DB::MappingException usage (#129)

This commit is contained in:
Johannes Müller 2021-10-13 01:51:53 +02:00 committed by GitHub
parent b1299fcada
commit 5a7d27e0c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 16 deletions

View file

@ -105,19 +105,19 @@ describe "DB::Serializable" do
end
it "should fail to initialize a simple model if types do not match" do
expect_raises ArgumentError do
expect_raises DB::MappingException, "Invalid Int32: b\n deserializing SimpleModel#c0" do
from_dummy("b,a", SimpleModel)
end
end
it "should fail to initialize a simple model if there is a missing column" do
expect_raises DB::MappingException do
expect_raises DB::MappingException, "Missing column c1\n deserializing SimpleModel#c1" do
from_dummy("1", SimpleModel)
end
end
it "should fail to initialize a simple model if there is an unexpected column" do
expect_raises DB::MappingException do
expect_raises DB::MappingException, "Unknown column: c2\n deserializing SimpleModel" do
from_dummy("1,a,b", SimpleModel)
end
end

View file

@ -6,6 +6,19 @@ module DB
end
class MappingException < Error
getter klass
getter property
def initialize(message, @klass : String, @property : String? = nil, cause : Exception? = nil)
message = String.build do |io|
io << message
io << "\n deserializing " << @klass
if property = @property
io << "#" << property
end
end
super(message, cause: cause)
end
end
class PoolTimeout < Error

View file

@ -117,7 +117,7 @@ module DB
{% end %}
else
{% if strict %}
raise ::DB::MappingException.new("unknown result set attribute: #{col_name}")
raise ::DB::MappingException.new("unknown result set attribute: #{col_name}", self.class.to_s)
{% else %}
%rs.read
{% end %}
@ -127,7 +127,7 @@ module DB
{% for key, value in properties %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key.id}.is_a?(Nil) && !%found{key.id}
raise ::DB::MappingException.new("missing result set attribute: {{(value[:key] || key).id}}")
raise ::DB::MappingException.new("missing result set attribute: {{(value[:key] || key).id}}", self.class.to_s)
end
{% end %}
{% end %}

View file

@ -129,6 +129,7 @@ module DB
{% for name, value in properties %}
when {{value[:key]}}
%found{name} = true
begin
%var{name} =
{% if value[:converter] %}
{{value[:converter]}}.from_rs(rs)
@ -137,6 +138,9 @@ module DB
{% else %}
rs.read({{value[:type]}})
{% end %}
rescue exc
::raise ::DB::MappingException.new(exc.message, self.class.to_s, {{name.stringify}}, cause: exc)
end
{% end %}
else
rs.read # Advance set, but discard result
@ -146,8 +150,8 @@ module DB
{% for key, value in properties %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key}.is_a?(Nil) && !%found{key}
raise ::DB::MappingException.new("missing result set attribute: {{(value[:key] || key).id}}")
if %var{key}.nil? && !%found{key}
::raise ::DB::MappingException.new("Missing column {{value[:key].id}}", self.class.to_s, {{key.stringify}})
end
{% end %}
{% end %}
@ -169,7 +173,7 @@ module DB
end
protected def on_unknown_db_column(col_name)
raise ::DB::MappingException.new("unknown result set attribute: #{col_name}")
::raise ::DB::MappingException.new("Unknown column: #{col_name}", self.class.to_s)
end
module NonStrict