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:, failure_log_policy: nil) ⇒ void

Builds a callback set for request/reply endpoint outcomes.

Parameters:

  • logger (#error)

    Logger used by the default failure log policy.

  • failure_log_policy (#call, nil) (defaults to: nil)

    Optional policy called with a failure payload before it is returned to the requester.



14
15
16
17
18
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 14

def initialize(logger:, failure_log_policy: nil)
  @failure_log_policy = failure_log_policy || lambda do |failure|
    logger.error 'Error processing message: ', failure
  end
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.



23
24
25
26
27
28
29
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 23

def callbacks
  {
    on_success: method(:respond_with_success),
    on_failure: method(:respond_with_failure),
    on_error: method(:respond_with_error),
  }
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.



60
61
62
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 60

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.



49
50
51
52
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 49

def respond_with_failure(wrapper, result)
  @failure_log_policy.call(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.



39
40
41
# File 'lib/leopard/nats_request_reply_callbacks.rb', line 39

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