error_sql.rb

lib/sequel/extensions/error_sql.rb
Last Update: 2016-05-12 12:28:13 -0700

The error_sql extension adds a DatabaseError#sql method that you can use to get the sql that caused the error to be raised.

begin
  DB.run "Invalid SQL"
rescue => e
  puts e.sql # "Invalid SQL"
end

On some databases, the error message contains part or all of the SQL used, but on other databases, none of the SQL used is displayed in the error message, so it can be difficult to track down what is causing the error without using a logger. This extension should hopefully make debugging easier on databases that have bad error messages.

This extension may not work correctly in the following cases:

  • log_connection_yield is not used when executing the query.

  • The underlying exception is frozen or reused.

  • The underlying exception doesn't correctly record instance variables set on it (seems to happen on JRuby when underlying exception objects are Java exceptions).

To load the extension into the database:

DB.extension :error_sql

Related module: Sequel::ErrorSQL