Better error message when requesting a driver that was not found. Fixes #21

This commit is contained in:
Ary Borenszweig 2016-10-21 11:24:32 -03:00
parent 0735870418
commit 56aa10c54d
2 changed files with 12 additions and 2 deletions

View file

@ -81,4 +81,10 @@ describe DB do
db.scalar "bar" db.scalar "bar"
end end
end end
it "gives nice error message when no driver is registered for schema (#21)" do
expect_raises(ArgumentError, %(no driver was registered for the schema "foobar", did you maybe forget to require the database driver?)) do
DB.open "foobar://baz"
end
end
end end

View file

@ -81,14 +81,18 @@ module DB
# :nodoc: # :nodoc:
def self.driver_class(driver_name) : Driver.class def self.driver_class(driver_name) : Driver.class
@@drivers.not_nil![driver_name] drivers[driver_name]? ||
raise(ArgumentError.new(%(no driver was registered for the schema "#{driver_name}", did you maybe forget to require the database driver?)))
end end
# Registers a driver class for a given *driver_name*. # Registers a driver class for a given *driver_name*.
# Should be called by drivers implementors only. # Should be called by drivers implementors only.
def self.register_driver(driver_name, driver_class : Driver.class) def self.register_driver(driver_name, driver_class : Driver.class)
drivers[driver_name] = driver_class
end
private def self.drivers
@@drivers ||= {} of String => Driver.class @@drivers ||= {} of String => Driver.class
@@drivers.not_nil![driver_name] = driver_class
end end
# Opens a database using the specified *uri*. # Opens a database using the specified *uri*.