Module: ServiceObjects::Helpers::Exceptions

Includes:
Messages
Included in:
Base
Defined in:
lib/service_objects/helpers/exceptions.rb

Overview

Note:

A target class should *include* the module

Features for escaping from runtime errors

Instance Method Summary collapse

Methods included from Messages

#add_message, #messages, #translate

Instance Method Details

#escape { ... } ⇒ Object

Re-raises standard errors as ServiceObjects::Invalid

Mutates the current object by adding error messages

Examples:

class MyClass
  includes ServiceObjects::Helpers::Exceptions
end

begin
  MyClass.new.escape { fail StandardError.new "foo" }
rescue => err
  puts err.class.name
  puts messages
end

# => ServiceObjects::Invalid
# => [<ServiceObject::Message type="error" text="foo" ...>]

Yields:

  • the block

Returns:

  • (Object)

    the value returned by the block

Raises:


38
39
40
41
42
43
# File 'lib/service_objects/helpers/exceptions.rb', line 38

def escape
  yield if block_given?
rescue => error
  collect_messages_from error
  raise Invalid.new(self)
end