Skip to content

Failures

In a simple use case, all service failures (or exceptions) will come from input, internal, or output. That would be considered as unexpected behavior in service operation.

But in addition to this, you can also describe expected errors in the service. The methods presented below are provided for this.

Method fail!

The basic fail! method allows you to pass the error text, as well as additional information through the meta attribute.

When calling a service through the .call! method, an exception with the class Servactory::Errors::Failure will occur.

ruby
make :check!

def check!
  return if inputs.invoice_number.start_with?("AA")

  fail!(message: "Invalid invoice number")
end
ruby
fail!(
  message: "Invalid invoice number",
  meta: {
    invoice_number: inputs.invoice_number
  }
)
ruby
exception.detailed_message  # => Invalid invoice number (ApplicationService::Errors::Failure)
exception.message           # => Invalid invoice number
exception.type              # => :fail
exception.meta              # => {:invoice_number=>"BB-7650AE"}

Method fail_input!

It differs from fail! by the mandatory indication of the name of the input attribute on behalf of which the error will be created.

When calling a service through the .call! method, an exception with the class Servactory::Errors::InputError will be thrown.

ruby
make :check!

def check!
  return if inputs.invoice_number.start_with?("AA")

  fail_input!(:invoice_number, message: "Invalid invoice number")
end