class FService::Result::Failure
Represents a value of a failed operation. The error field can contain any information you want.
@!attribute [r] error
@return [Object] the provided error for the result
@!attribute [r] type
@return [Object] the provided type for the result. Defaults to nil.
@api public
Attributes
Public Class Methods
Creates a failed operation. You usually shouldn't call this directly. See {FService::Base#Failure}.
@param error [Object] failure value.
# File lib/f_service/result/failure.rb, line 23 def initialize(error, type = nil) @error = error @type = type freeze end
Public Instance Methods
Returns itself to the given block. Use this to chain multiple service calls (since all services return Results). It will short circuit your service call chain.
@example
class UsersController < BaseController def create result = User::Create.(user_params) # if this fails the following calls won't run .and_then { |user| User::SendWelcomeEmail.(user: user) } .and_then { |user| User::Login.(user: user) } if result.successful? json_success(result.value) else json_error(result.error) end end end
@return [self]
# File lib/f_service/result/failure.rb, line 110 def and_then self end
Returns the current error to the given block. Use this to chain multiple service calls (since all services return Results). It works just like the `.and_then` method, but only runs if the result is a Failure
.
@example
class UpdateUserOnExternalService attribute :user_params def run check_api_status .and_then { update_user } .or_else { create_update_worker } end private # some code end
@yieldparam error pass {#error} to a block @yieldparam type pass {#type} to a block
# File lib/f_service/result/failure.rb, line 83 def catch yield(*to_ary) end
Returns true.
@example
# Suppose that User::Update returns an FService::Result log_errors(user) if User::Update.(user: user).failed?
# File lib/f_service/result/failure.rb, line 47 def failed? true end
Returns false.
@example
# Suppose that User::Update returns an FService::Result log_errors(user) unless User::Update.(user: user).successful?
# File lib/f_service/result/failure.rb, line 36 def successful? false end
Outputs a string representation of the object
@example
puts FService::Result::Failure.new("Oh no!") # => Failure("Oh no!")
@return [String] the object's string representation
# File lib/f_service/result/failure.rb, line 123 def to_s error.nil? ? 'Failure()' : "Failure(#{error.inspect})" end
Failed operations do not have value.
# File lib/f_service/result/failure.rb, line 52 def value nil end
Raises an exception if called. (see value
)
# File lib/f_service/result/failure.rb, line 58 def value! raise Result::Error, 'Failure objects do not have value' end