Catch errors during websocket send

Tidy up the error handling code a little, and substitute out gem paths.
This commit is contained in:
Samuel Cochran 2016-08-10 09:34:09 +10:00
parent 132f1c0f42
commit e8531da70d
No known key found for this signature in database
GPG Key ID: 1A36ACA1BDECD23E
3 changed files with 22 additions and 10 deletions

View File

@ -60,6 +60,17 @@ module MailCatcher extend self
end end
end end
def log_exception(message, context, exception)
gems_paths = (Gem.path | [Gem.default_dir]).map { |path| Regexp.escape(path) }
gems_regexp = %r{(?:#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)}
gems_replace = '\1 (\2) \3'
puts "*** #{message}: #{context.inspect}"
puts " Exception: #{exception}"
puts " Backtrace:", *exception.backtrace.map { |line| " #{line.sub(gems_regexp, gems_replace)}" }
puts " Please submit this as an issue at http://github.com/sj26/mailcatcher/issues"
end
@@defaults = { @@defaults = {
:smtp_ip => '127.0.0.1', :smtp_ip => '127.0.0.1',
:smtp_port => '1025', :smtp_port => '1025',

View File

@ -46,14 +46,8 @@ class MailCatcher::Smtp < EventMachine::Protocols::SmtpServer
MailCatcher::Mail.add_message current_message MailCatcher::Mail.add_message current_message
puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)" puts "==> SMTP: Received message from '#{current_message[:sender]}' (#{current_message[:source].length} bytes)"
true true
rescue rescue => exception
puts "*** Error receiving message: #{current_message.inspect}" MailCatcher.log_exception("Error receiving message", @current_message, exception)
puts " Exception: #{$!}"
puts " Backtrace:"
$!.backtrace.each do |line|
puts " #{line}"
end
puts " Please submit this as an issue at http://github.com/sj26/mailcatcher/issues"
false false
ensure ensure
@current_message = nil @current_message = nil

View File

@ -64,8 +64,15 @@ module MailCatcher
if request.websocket? if request.websocket?
request.websocket!( request.websocket!(
:on_start => proc do |websocket| :on_start => proc do |websocket|
subscription = Events::MessageAdded.subscribe { |message| websocket.send_message(JSON.generate(message)) } subscription = Events::MessageAdded.subscribe do |message|
websocket.on_close do |websocket| begin
websocket.send_message(JSON.generate(message))
rescue => exception
MailCatcher.log_exception("Error sending message through websocket", message, exception)
end
end
websocket.on_close do |*|
Events::MessageAdded.unsubscribe subscription Events::MessageAdded.unsubscribe subscription
end end
end) end)