Sha256: 60f40e248798d0a14109eec96f3828f1fe6f328c495a83ca791ed0977b907459

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require_relative "message"

module Hexx
  class Service

    # The exception to be raised 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::Service::Invalid}.
    class Invalid < ::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::Service::Invalid.new service
      #
      # @param  [Hexx::Service] service The invalid service.
      # @raise  [ArgumentError] if the argument is not a service object.
      # @return [Hexx::Service::Invalid] The exception.

      # @api hide
      def initialize(service)
        @service = service
        fail ArgumentError unless self.service.is_a? Hexx::Service
      end

      # @!attribute [r] message
      # Returns a default text message for the exception.
      #
      # @example
      #   error = Hexx::Service::Invalid.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
      #   error = Hexx::Service::Invalid.new service
      #   error.messages            # => [#<Hexx::Message... >]
      #   error.messages.first.type # => "error"
      #
      # @return [Array<Hexx::Service::Message>] The list of messages.
      def messages
        service.errors.values.flatten.map do |text|
          Message.new type: "error", text: text
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hexx-3.2.0 lib/hexx/service/invalid.rb
hexx-3.1.0 lib/hexx/service/invalid.rb
hexx-3.0.0 lib/hexx/service/invalid.rb