Class: Servus::Support::Rescuer::BlockContext Private

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/support/rescuer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides success/failure methods to rescue_from blocks.

This context is used when a rescue_from block is executed. It provides the same success() and failure() methods available in service call methods, allowing blocks to create appropriate Response objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlockContext

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of BlockContext.



40
41
42
# File 'lib/servus/support/rescuer.rb', line 40

def initialize
  @result = nil
end

Instance Attribute Details

#resultServus::Support::Response? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The response created by success() or failure().

Returns:



82
83
84
# File 'lib/servus/support/rescuer.rb', line 82

def result
  @result
end

Instance Method Details

#failure(message = nil, type: Servus::Support::Errors::ServiceError) ⇒ Servus::Support::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a failure response.

Use this in rescue_from blocks to convert exceptions into business failures with custom messages and error types.

Examples:

rescue_from ActiveRecord::RecordInvalid do |exception|
  failure("Database error: #{exception.message}", type: InternalServerError)
end

Parameters:

  • message (String, nil) (defaults to: nil)

    The error message (uses error type's DEFAULT_MESSAGE if nil)

  • type (Class<Servus::Support::Errors::ServiceError>) (defaults to: Servus::Support::Errors::ServiceError)

    The error type

Returns:



73
74
75
76
# File 'lib/servus/support/rescuer.rb', line 73

def failure(message = nil, type: Servus::Support::Errors::ServiceError)
  error = type.new(message)
  @result = Response.new(false, nil, error)
end

#success(data = nil) ⇒ Servus::Support::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a success response.

Use this in rescue_from blocks to recover from exceptions and return successful results despite the error being raised.

Examples:

rescue_from SomeError do |exception|
  success(recovered: true, original_error: exception.message)
end

Parameters:

  • data (Hash, Object) (defaults to: nil)

    The success data to return

Returns:



56
57
58
# File 'lib/servus/support/rescuer.rb', line 56

def success(data = nil)
  @result = Response.new(true, data, nil)
end