module Sequel::Plugins::ThrowFailures

  1. lib/sequel/plugins/throw_failures.rb

The throw_failures plugin throws HookFailed and ValidationFailed exceptions instead of raising them. If there is no matching catch block, the UncaughtThrowError will be rescued and the HookFailed or ValidationFailed exception will be raised normally.

If you are setting up the catch blocks to handle these failures, in the failure case this plugin is about 10-15% faster on CRuby and 10x faster on JRuby. If you are not setting up the catch blocks, in the failure case this plugin is about 30% slower on CRuby and 2x slower on JRuby. So this plugin should only be used if you are setting up catch blocks manually.

This plugin will setup catch blocks automatically for internally rescued HookFailed exceptions when the model is configured to not raise exceptions on failure (by default, the exceptions are internally rescued in that case.

To set up the catch blocks, use the class of the exception:

ret = catch(Sequel::ValidationFailed) do
  model_instance.save
end
if ret.is_a?(Sequel::ValidationFailed)
  # handle failure
else
  # handle success
end

Usage:

# Make all model subclass instances throw HookFailed and ValidationFailed exceptions
# (called before loading subclasses)
Sequel::Model.plugin :throw_failures

# Make the Album class throw HookFailed and ValidationFailed exceptions
Album.plugin :throw_failures