lib/protobuf/nats.rb in protobuf-nats-0.3.1 vs lib/protobuf/nats.rb in protobuf-nats-0.3.2
- old
+ new
@@ -40,10 +40,36 @@
end
# Eagerly load the yml config.
config
+ # We will always log an error.
+ def self.error_callbacks
+ @error_callbacks ||= [lambda { |error| log_error(error) }]
+ end
+
+ # Eagerly load the yml config.
+ error_callbacks
+
+ def self.on_error(&block)
+ fail ::ArgumentError unless block.arity == 1
+ error_callbacks << block
+ nil
+ end
+
+ def self.notify_error_callbacks(error)
+ error_callbacks.each do |callback|
+ begin
+ callback.call(error)
+ rescue => callback_error
+ log_error(callback_error)
+ end
+ end
+
+ nil
+ end
+
def self.subscription_key(service_klass, service_method)
service_class_name = service_klass.name.underscore.gsub("/", ".")
service_method_name = service_method.to_s.underscore
subscription_key = "rpc.#{service_class_name}.#{service_method_name}"
@@ -81,10 +107,10 @@
@client_nats_connection.on_close do
logger.warn("Client NATS connection was closed")
end
@client_nats_connection.on_error do |error|
- log_error(error)
+ notify_error_callbacks(error)
end
true
end
end