On Tuesday, August 7, 2018 at 8:14:10 AM UTC-7, Alexander Popov wrote:
>
> Hello!
>
> I see in the documentation of transactions that `Sequel::Rollback`
> exception will be suppressed without `reraise` option, and any another
> exception will not.
>
> But `Database#after_rollback` suppresses any exception, not only
> `Sequel::Rollback`.
>
Database#after_rollback does not suppress any exception:
ruby bin/sequel -E sqlite:/ -c 'DB.transaction{DB.after_rollback{puts
:ar}; raise} rescue (p $!)'
I, [2018-08-07T09:47:19.102175 #86248] INFO -- : (0.000537s) PRAGMA
foreign_keys = 1
I, [2018-08-07T09:47:19.102459 #86248] INFO -- : (0.000096s) PRAGMA
case_sensitive_like = 1
I, [2018-08-07T09:47:19.103353 #86248] INFO -- : (0.000368s) SELECT
sqlite_version()
I, [2018-08-07T09:47:19.103585 #86248] INFO -- : (0.000059s) BEGIN
I, [2018-08-07T09:47:19.103806 #86248] INFO -- : (0.000039s) ROLLBACK
ar
RuntimeError
Note how the RuntimeError raised inside the transaction block is not
supressed, and after_rollback is called.
after_rollback is called in an ensure block after the transaction has been
rolled back, it does not rescue any exceptions.
> So, if I have some custom check in transaction and manual conditional
> `raise Sequel::Rollback unless valid?` with `after_rollback { return
> validation_errors }` — I'll not get any another exception like
> `Sequel::MassAssignmentRestriction`.
>
> Can be `Database#after_rollback` called only after `Sequel::Rollback`
> exception?
>
No, that wouldn't make sense. after_rollback should be called after any
transaction rollback, regardless of the cause.
If you want to handle conditional behavior in your after_rollback, you
could do:
after_rollback { return validation_errors if !$! ||
$!.is_a?(Sequel::Rollback) }
$! will be nil in the case that Sequel::Rollback is suppressed.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.