Sha256: ea0e7a19863a1afa478d53ce483ec5b1982fd6ff78c20001c9f12b5130302eba

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Sanford; end
module Sanford::Protocol

  class BadMessageError < RuntimeError
    def initialize(message, bt = nil)
      super(message)
      set_backtrace(bt || caller)
    end
  end

  class MsgData
    attr_reader :value

    def initialize(called_from = nil, &get_value)

      # By default, any exceptions from getting the value are "hidden" behind a
      # more general `BadMessageError`. In non-debug scenarios this is ideal and
      # allows you to rescue from a common exception and respond in a standard way.
      # In debug scenarios, however, go ahead and raise the real exception.

      begin
        @value = get_value.call
      rescue Exception => err
        @called_from = called_from || caller
        ENV['SANFORD_PROTOCOL_DEBUG'] ? raise(err) : self.error!(self.get_value_error)
      end
    end

    def get_value_error; "Error reading the message"; end
    def validate!; self; end

    def error!(message)
      raise BadMessageError.new(message, @called_from)
    end

  end

  class MsgSize < MsgData
    def get_value_error; "Error reading message body size."; end
    def validate!
      error!("Empty message size") if self.value.nil?
      super
    end
  end

  class MsgVersion < MsgData
    def get_value_error; "Error reading message protocol version"; end
    def validate!
      error!("Protocol version mismatch") if version_mismatch?
      super
    end

    def version_mismatch?
      self.value != Sanford::Protocol.msg_version
    end
  end

  class MsgBody < MsgData
    def get_value_error; "Error reading message body."; end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sanford-protocol-0.12.0 lib/sanford-protocol/msg_data.rb