Sha256: 86d3b3d6d1054a432514fa58f2a038a59b1fc7cb4ba73a8875201a96fb7d2791

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

require_relative "message"

module Hexx

  # The exception to be risen by invalid services.
  #
  # @example
  #   service GetItem < Hexx::Service
  #     allow_params :uuid
  #     validates :uuid, presence: true
  #     def run
  #       validate!
  #     end
  #   end
  #
  #   service = GetItem.new
  #   service.run # => fails with the {Hexx::ServiceInvalid}.
  class ServiceInvalid < ::RuntimeError

    # @!attribute [r] service
    # @return [Hexx::Service] The invalid service object.
    attr_reader :service

    # @!scope class
    # @!method new(service)
    # Initializes the exception.
    #
    # @example
    #   fail Hexx::ServiceInvalid.new service
    #
    # @param  [Hexx::Service] service The invalid service.
    # @raise  [ArgumentError] if the argument is not a service object.
    # @return [Hexx::ServiceInvalid] The exception.

    # @api hide
    def initialize(service)
      @service = service
    end

    # @!attribute [r] message
    # Returns a default text message for the exception.
    #
    # @example
    #   error = Hexx::ServiceInvalid.new service
    #   error.message # => "Service invalid: #<Hexx::Service... >"
    #
    # @return [String] The message.
    def message
      "Service invalid: #{ service.inspect }"
    end

    # @!attribute [r] messages
    # Returns a list of error messages from the service.
    #
    # @example
    #   service.errors.add :base, "some error"
    #
    #   error = Hexx::ServiceInvalid.new service
    #   error.messages
    #   # => [#<Hexx::Message @type="error" @text="some error" >]
    #
    # @return [Array<Hexx::Message>] The list of error messages.
    def messages
      errors.map { |text| Message.new type: "error", text: text }
    end

    private

    def errors
      service.errors.messages.values.flatten
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hexx-7.1.0 lib/hexx/service_invalid.rb
hexx-7.0.1 lib/hexx/service_invalid.rb
hexx-7.0.0 lib/hexx/service_invalid.rb