I have a Sinatra app utilizing Sequel and Postgres... it's a very simple module that inserts into a database. I want to capture any errors from the insert and return a useful message.
My codes is as follows:
begin
sql = DB["INSERT INTO table (id, firstname, lastname, ...) values (......)"]
ds.insert
rescue Sequel::Error
...
end
How do I capture what the actual error is? I can put "There was an error" and that is printed, but I want something more specific - like "First name is required", "Last name is required".
Can someone help?
You can use the last exception object magic constant $!:
rescue Sequel::Error
p $!.message
end
Also you can change the rescue block to put the exception object into a variable:
rescue Sequel::Error => e
p e.message
end
Both would print the exception messsage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With