Class: Rubyists::Leopard::NatsRequestReplyCallbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/leopard/nats_request_reply_callbacks.rb

Overview

Maps Leopard handler outcomes to request/reply response behavior.

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ void

Builds a callback set for request/reply endpoint outcomes.

Parameters:

  • logger (#error)

    Logger used for failure payloads.



12
13
14
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 12

def initialize(logger:)
  @logger = logger
end

Instance Method Details

#callbacksHash{Symbol => #call}

Returns transport callbacks for request/reply endpoints.

Returns:

  • (Hash{Symbol => #call})

    Outcome callbacks keyed by ‘:on_success`, `:on_failure`, and `:on_error`.



19
20
21
22
23
24
25
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 19

def callbacks
  {
    on_success: method(:respond_with_success),
    on_failure: method(:respond_with_failure),
    on_error: method(:respond_with_error),
  }
end

#log_failure(failure) ⇒ void (private)

This method returns an undefined value.

Logs the failure payload returned by a handler.

Parameters:

  • failure (Object)

    The failure payload from the handler.



65
66
67
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 65

def log_failure(failure)
  @logger.error 'Error processing message: ', failure
end

#respond_with_error(wrapper, error) ⇒ void (private)

This method returns an undefined value.

Responds to a request with an exception payload after an unhandled error.

Parameters:

  • wrapper (MessageWrapper)

    Wrapped request message.

  • error (StandardError)

    The unhandled exception.



56
57
58
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 56

def respond_with_error(wrapper, error)
  wrapper.respond_with_error(error)
end

#respond_with_failure(wrapper, result) ⇒ void (private)

This method returns an undefined value.

Responds to a failed request with the failure payload.

Parameters:

  • wrapper (MessageWrapper)

    Wrapped request message.

  • result (Dry::Monads::Failure)

    Failed handler result.



45
46
47
48
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 45

def respond_with_failure(wrapper, result)
  log_failure(result.failure)
  wrapper.respond_with_error(result.failure)
end

#respond_with_success(wrapper, result) ⇒ void (private)

This method returns an undefined value.

Responds to a successful request with the handler payload.

Parameters:

  • wrapper (MessageWrapper)

    Wrapped request message.

  • result (Dry::Monads::Success)

    Successful handler result.



35
36
37
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 35

def respond_with_success(wrapper, result)
  wrapper.respond(result.value!)
end