app/controllers/concerns/exceptions.rb
# handle errors/exceptions/non-happy-path-events in a systematic way
handle errors/exceptions/non-happy-path-events in a systematic way
handle errors / exceptions / non - happy - path - events in a systematic way
def handle_exception (e, msg_to_user="", msg_to_log="", send_email=false)
    error_msg = "=====\n"
    error_msg += "Error\n"
    error_msg += "=====\n\n"
    error_msg += "Script(s)\n"
    error_msg += "---------\n"
    error_msg += e.backtrace.select{ |i| i.include?(Rails.root.to_s) and i.exclude?("bundle") }.join("\n") + "\n\n"
    error_msg += "Message\n"
    error_msg += "-------\n"
    error_msg += e.message + "\n\n"
    error_msg += "Additional info\n---------------\n" + msg_to_log + "\n\n" unless msg_to_log.empty?
    if request and user_signed_in? and current_user and current_user.email
      error_msg += "User\n----\n" + current_user.email + "\n\n" 
    end
    Rails.logger.error error_msg
    flash[:error] = msg_to_user if request
    if send_email
      begin
        RdMailer.notify_admin_about_error(error_msg).deliver_later
      rescue => e2
        Rails.logger.error "Also an error sending the error email: " + e2.message
      end
    end
  end
def handle_exception ( e , msg_to_user = "" , msg_to_log = "" , send_email = false ) error_msg = "=====\n" error_msg += "Error\n" error_msg += "=====\n\n" error_msg += "Script(s)\n" error_msg += "---------\n" error_msg += e . backtrace . select { | i | i . include? ( Rails . root . to_s ) and i . exclude? ( "bundle" ) } . join ( "\n" ) + "\n\n" error_msg += "Message\n" error_msg += "-------\n" error_msg += e . message + "\n\n" error_msg += "Additional info\n---------------\n" + msg_to_log + "\n\n" unless msg_to_log . empty? if request and user_signed_in? and current_user and current_user . email error_msg += "User\n----\n" + current_user . email + "\n\n" end Rails . logger . error error_msg flash [ :error ] = msg_to_user if request if send_email begin RdMailer . notify_admin_about_error ( error_msg ) . deliver_later rescue => e2 Rails . logger . error "Also an error sending the error email: " + e2 . message end end end
handle errors/exceptions/non-happy-path-events in a systematic way
handle errors / exceptions / non - happy - path - events in a systematic way
# create pretty error message # log the error # present the user with an error message so long as this is occured via a web request  # send an error email if appropriate # if there's an error in the mail routine, just log that there was an error
e
<not_specific>
msg_to_user
<not_specific>
msg_to_log
<not_specific>
send_email
<not_specific>