Skip to content

Commit

Permalink
Exceptions on subscriptions
Browse files Browse the repository at this point in the history
Raising an exception on a block call is useless, as the user can't catch them, instead catch those exceptions and log the problem.
  • Loading branch information
adriacidre committed Jul 19, 2024
1 parent 1e491d0 commit 1174df1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
21 changes: 16 additions & 5 deletions lib/services/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def on_message(opts = {}, &block)
cm.mark_as_delivered unless opts[:mark_as_delivered] == false
cm.mark_as_read if opts[:mark_as_read] == true

block.call(cm)
call(cm, block)
end
end

Expand Down Expand Up @@ -104,19 +104,19 @@ def delete(recipients, cids, gid = nil)
def on_invite(&block)
@messaging.subscribe :chat_invite do |msg|
g = SelfSDK::Chat::Group.new(self, msg.payload)
block.call(g)
call(g, block)
end
end

def on_join(&block)
@messaging.subscribe :chat_join do |msg|
block.call(iss: msg.payload[:iss], gid: msg.payload[:gid])
call({iss: msg.payload[:iss], gid: msg.payload[:gid]}, block)
end
end

def on_leave(&block)
@messaging.subscribe :chat_remove do |msg|
block.call(iss: msg.payload[:iss], gid: msg.payload[:gid])
call({iss: msg.payload[:iss], gid: msg.payload[:gid]}, block)
end
end

Expand Down Expand Up @@ -194,7 +194,7 @@ def generate_connection_deep_link(callback, opts = {})
# @yield [request] Invokes the block with a connection response message.
def on_connection(&block)
@messaging.subscribe :connection_response do |msg|
block.call(msg)
call(msg, block)
end
end

Expand Down Expand Up @@ -243,6 +243,17 @@ def create_missing_sessions(members)
posterior_members = true if m == @app_id
end
end

def call(m, &block)
block.call(cm)
rescue StandardError => e
SelfSDK.logger.error("A StandardError occurred: #{e.message}")
SelfSDK.logger.error("Backtrace:\n\t#{e.backtrace.join("\n\t")}")
rescue Exception => e
SelfSDK.logger.error("An unexpected exception occurred: #{e.message}")
SelfSDK.logger.error("Backtrace:\n\t#{e.backtrace.join("\n\t")}")
end

end
end
end
61 changes: 37 additions & 24 deletions lib/services/voice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ def setup(recipient, name, cid)
# Subscribes to chat.voice.setup messages.
def on_setup(&block)
@messaging.subscribe :voice_setup do |msg|
block.call(msg.payload[:iss], msg.payload[:cid], msg.payload[:data])
call({ msg.payload[:iss],
msg.payload[:cid],
msg.payload[:data] }, block)
end
end

Expand All @@ -46,17 +48,17 @@ def start(recipient, cid, call_id, peer_info, data)
# Subscribes to chat.voice.start messages.
def on_start(&block)
@messaging.subscribe :voice_start do |msg|
block.call(msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info],
data: msg.payload[:data])
call({ msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info],
data: msg.payload[:data] }, block)
end
end

# Sends a chat.voice.accept message accepting a specific call.
def accept(recipient, cid, call_id, peer_info)
payload = {
payload = {
typ: SelfSDK::Messages::VoiceAccept::MSG_TYPE,
cid: cid,
call_id: call_id,
Expand All @@ -68,16 +70,16 @@ def accept(recipient, cid, call_id, peer_info)
# Subscribes to chat.voice.accept messages.
def on_accept(&block)
@messaging.subscribe :voice_accept do |msg|
block.call(msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info])
call({ msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info] }, block)
end
end

# Sends a chat.voice.accept message finishing the call.
def stop(recipient, cid, call_id)
payload = {
payload = {
typ: SelfSDK::Messages::VoiceStop::MSG_TYPE,
cid: cid,
call_id: call_id,
Expand All @@ -88,10 +90,10 @@ def stop(recipient, cid, call_id)
# Subscribes to chat.voice.stop messages.
def on_stop(&block)
@messaging.subscribe :voice_stop do |msg|
block.call(msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info])
call({ msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info] }, block)
end
end

Expand All @@ -106,10 +108,10 @@ def busy(recipient, cid, call_id)
# Subscribes to chat.voice.busy messages.
def on_busy(&block)
@messaging.subscribe :voice_busy do |msg|
block.call(msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info])
call({ msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info] }, block)
end
end

Expand All @@ -124,10 +126,10 @@ def summary(recipient, cid, call_id)
# Subscribes to chat.voice.summary messages.
def on_summary(&block)
@messaging.subscribe :voice_summary do |msg|
block.call(msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info])
call({ msg.payload[:iss],
cid: msg.payload[:cid],
call_id: msg.payload[:call_id],
peer_info: msg.payload[:peer_info] }, block)
end
end

Expand All @@ -142,6 +144,17 @@ def send(recipients, body)
end
m
end

def call(m, &block)
block.call(cm)
rescue StandardError => e
SelfSDK.logger.error("A StandardError occurred: #{e.message}")
SelfSDK.logger.error("Backtrace:\n\t#{e.backtrace.join("\n\t")}")
rescue Exception => e
SelfSDK.logger.error("An unexpected exception occurred: #{e.message}")
SelfSDK.logger.error("Backtrace:\n\t#{e.backtrace.join("\n\t")}")
end

end
end
end

0 comments on commit 1174df1

Please sign in to comment.