actor icon indicating copy to clipboard operation
actor copied to clipboard

Add custom classes support for `raise`

Open afuno opened this issue 1 year ago • 0 comments

This PR adds support for custom classes for use in raise.

This is necessary, for example, when you want to use the Actor gem to implement services, forms, or something else.

Base class examples:

class ApplicationService
  include ServiceActor::Base

  argument_error_class(MyCustomErrorForService::ArgumentError)
  failure_class(MyCustomErrorForService::Failure)
end
class ApplicationForm
  include ServiceActor::Base

  argument_error_class(MyCustomErrorForForm::ArgumentError)
  failure_class(MyCustomErrorForForm::Failure)
end

Usage example:

module MyCustomErrorForService
  class ArgumentError < ServiceActor::ArgumentError; end

  class Failure < ServiceActor::Failure; end
end
class ApplicationService
  include ServiceActor::Base

  define_argument_error_class(MyCustomErrorForService::ArgumentError)
  define_failure_class(MyCustomErrorForService::Failure)
end
class PayService < ApplicationService
  input :order, type: String, allow_nil: false

  play :pay_order

  private

  def pay_order
    fail!("Can't pay for an order!")
  end
end
PayService.call(order: nil)
MyCustomErrorForService::ArgumentError: The "order" input on "PayService" does not allow nil values

afuno avatar Sep 17 '22 12:09 afuno