Question

When using transactions in Sequel you are supposed to be able to raise an exception and have it bubble out of the transaction block.

From the Sequel documentation:

If any other exception is raised, the transaction is rolled back, and the exception is raised outside the block:

DB.transaction do # BEGIN
  raise ArgumentError
end # ROLLBACK
# ArgumentError raised

However, it seems I'm not able to catch the ArgumentError since Sequel wraps it in a Sequel::DatabaseError:

begin
  DB.transaction do
    raise ArgumentError.new('Hey')
  end
rescue => e
  puts e.inspect
  puts e.kind_of?(ArgumentError)
end

Result:

> #<Sequel::DatabaseError: ArgumentError: Hey>
> false

How can I solve this?

Was it helpful?

Solution

You could try using wrapped_exception as documented here and re-raise the exception.

begin
  DB.transaction do
    raise ArgumentError.new('Hey')
  end
rescue Sequel::DatabaseError => e
  raise e.wrapped_exception
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top