8
  begin

    ActiveRecord::Base.transaction do

      // ...
      sanitized_sql = "INSERT INTO pinfo ..."
      ActiveRecord::Base.connection.execute(sanitized_sql)

    end

  rescue

    // how can I get the error?       

  end

In the webrick console, the error (1967-07-16?00:00:00) is shown as:

EXECUTE (0.0ms) ODBC::Error: 22008 (241) [unixODBC][FreeTDS][SQL Server]Syntax error converting datetime from character string.: INSERT INTO pinfo (birthdate) VALUES ('1967-07-16?00:00:00') EXECUTE (0.8ms) IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION

How can I get the above error message (ODBC::Error: 22008 (241) ...) raised from ActiveRecord::Base.connection.execute in rescue?

ohho
  • 50,879
  • 75
  • 256
  • 383

1 Answers1

3
 begin  
  ActiveRecord::Base.transaction do  

  // ...
  sanitized_sql = "INSERT INTO pinfo ..."
  ActiveRecord::Base.connection.execute(sanitized_sql)

  end
 rescue Exception => exc 
   logger.error("Message for the log file #{exc.message}") 
   flash[:notice] = "Store error message #{exec.message}" 
 end

Thanks

Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • Consider changing it to rescue StandardException - https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby – iheggie May 19 '19 at 03:10