From 54cb609ae8eb61aa33c9ec1a2a928683210b8bb3 Mon Sep 17 00:00:00 2001 From: Didier BANLOCK Date: Mon, 28 Aug 2017 20:16:39 +0200 Subject: [PATCH 1/2] Email notification support added --- lib/rails_exception_handler/handler.rb | 2 ++ lib/rails_exception_handler/storage.rb | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/rails_exception_handler/handler.rb b/lib/rails_exception_handler/handler.rb index 98139f7..7d0abc2 100644 --- a/lib/rails_exception_handler/handler.rb +++ b/lib/rails_exception_handler/handler.rb @@ -32,6 +32,8 @@ def store_error RailsExceptionHandler::Storage.send(strategy, @parsed_error.external_info) elsif(strategy.class == Hash && strategy[:remote_url]) RailsExceptionHandler::Storage.remote_url(strategy[:remote_url][:target],@parsed_error.external_info) + elsif(strategy.class == Hash && strategy[:email]) + RailsExceptionHandler::Storage.email(strategy[:email][:recipients],@parsed_error.external_info) else raise "RailsExceptionHandler: Unknown storage strategy #{strategy.inspect}" end diff --git a/lib/rails_exception_handler/storage.rb b/lib/rails_exception_handler/storage.rb index 5999ccd..04d7bc1 100644 --- a/lib/rails_exception_handler/storage.rb +++ b/lib/rails_exception_handler/storage.rb @@ -28,7 +28,12 @@ def self.remote_url(target, info) params = flatten_hash({:error_message => info}) Net::HTTP::post_form(uri, params) end - + + # Notify application admin that an error occured + def self.email(recipients,info) + ErrorMailer.send_error_mail_to_admin(info.to_json,recipients).deliver_later unless recipients.blank? + end + private # Credit: Hash flattening technique borrowed from Peter Marklund: http://marklunds.com/articles/one/314 From 6cf140a62ea51cd95af8f70b2cd77c8124fb7270 Mon Sep 17 00:00:00 2001 From: Didier BANLOCK Date: Mon, 28 Aug 2017 21:24:37 +0200 Subject: [PATCH 2/2] mailer added --- app/mailers/error_mailer.rb | 13 ++++ .../send_error_mail_to_admin.html.erb | 61 +++++++++++++++++++ .../send_error_mail_to_admin.text.erb | 61 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 app/mailers/error_mailer.rb create mode 100644 app/views/error_mailer/send_error_mail_to_admin.html.erb create mode 100644 app/views/error_mailer/send_error_mail_to_admin.text.erb diff --git a/app/mailers/error_mailer.rb b/app/mailers/error_mailer.rb new file mode 100644 index 0000000..f0872a5 --- /dev/null +++ b/app/mailers/error_mailer.rb @@ -0,0 +1,13 @@ +class ErrorMailer < ApplicationMailer + + # Subject can be set in your I18n file at config/locales/en.yml + # with the following lookup: + # + # en.error_mailer.send_error_mail_to_admin.subject + # + def send_error_mail_to_admin(info,email) + @info = JSON.parse(info) + mail(to: email, subject: 'An error occured on your application') + end + +end diff --git a/app/views/error_mailer/send_error_mail_to_admin.html.erb b/app/views/error_mailer/send_error_mail_to_admin.html.erb new file mode 100644 index 0000000..0ae10f1 --- /dev/null +++ b/app/views/error_mailer/send_error_mail_to_admin.html.erb @@ -0,0 +1,61 @@ +

+ Hi boss, +

+

+ an error occured at <%= @info["created_at"] %> +

+ + +

+ TARGET URL
+ <%= @info["target_url"] %> +

+ + +<% unless @info["referer_url"].blank? %> +

+ REFERER URL
+ <%= @info["referer_url"] %> +

+<%end%> + +

+ PARAMETERS
+ <%= @info["params"] %> +

+ +

+ USER AGENT
+ <%= @info["user_agent"] %> +

+ +

+ USER INFORMATION
+ <%= @info["user_info"] %> +

+ +

+ CLASS NAME
+ <%= @info["class_name"] %> + +

+ +<% unless @info["message"].blank? %> +

+ MESSAGE
+ <%= @info["message"] %> +

+<%end%> + +

+ TRACE
+ <%= @info["trace"] %> +

+ + +<% unless @info["doc_root"].blank? %> +

+ DOC ROOT
+ <%= @info["doc_root"] %> +

+<%end%> \ No newline at end of file diff --git a/app/views/error_mailer/send_error_mail_to_admin.text.erb b/app/views/error_mailer/send_error_mail_to_admin.text.erb new file mode 100644 index 0000000..0ae10f1 --- /dev/null +++ b/app/views/error_mailer/send_error_mail_to_admin.text.erb @@ -0,0 +1,61 @@ +

+ Hi boss, +

+

+ an error occured at <%= @info["created_at"] %> +

+ + +

+ TARGET URL
+ <%= @info["target_url"] %> +

+ + +<% unless @info["referer_url"].blank? %> +

+ REFERER URL
+ <%= @info["referer_url"] %> +

+<%end%> + +

+ PARAMETERS
+ <%= @info["params"] %> +

+ +

+ USER AGENT
+ <%= @info["user_agent"] %> +

+ +

+ USER INFORMATION
+ <%= @info["user_info"] %> +

+ +

+ CLASS NAME
+ <%= @info["class_name"] %> + +

+ +<% unless @info["message"].blank? %> +

+ MESSAGE
+ <%= @info["message"] %> +

+<%end%> + +

+ TRACE
+ <%= @info["trace"] %> +

+ + +<% unless @info["doc_root"].blank? %> +

+ DOC ROOT
+ <%= @info["doc_root"] %> +

+<%end%> \ No newline at end of file