Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing Sequel::Error exceptions

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?

like image 868
Dan Brooking Avatar asked Dec 01 '25 09:12

Dan Brooking


1 Answers

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.

like image 53
Sigurd Avatar answered Dec 04 '25 00:12

Sigurd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!