dimanche 22 mars 2015

Get the string inside the ActiveRecord::StatementInvalid rescue message

I'm trying to get the middle string inside the standard error message given by the rails rescue.


I have created BEFORE INSERT Triggers on my NuoDB database that further validates records on the database side and not just on the rails side.


What this trigger does is compare two dates when saving a record. If the first date is later than the 2nd date, the trigger will throw the following JDBC error string:



"INPUT_ERROR - Invalid Dates: date_2 must be greater than date_1"


Then I have the following code on the controller to save a record



def foo
begin
# save record
rescue => e
flash[:notice] = e
end
end


So saving a new record with incorrect dates would give a flash[:notice] of the error which would be something like



ActiveRecord::JDBCError: INPUT_ERROR - Invalid Dates: date_2 must be greater than date_1: INSERT INTO `table` (`date_1`, `date_2`) VALUES ('2016-03-23','2015-03-23')


What I would like to get is just the custom text I created. I would like to get the string without the "ActiveRecord::JDBCError:" string at the start and the SQL statement at the end.


So how can I get the text so that my flash[:notice] would just output this:



"INPUT_ERROR - Invalid Dates: date_2 must be greater than date_1"

Aucun commentaire:

Enregistrer un commentaire