From 5977aa37af883a76bb9fc91d16188bc2b8d35ee2 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 19 Mar 2021 19:41:03 -0300 Subject: [PATCH] =?UTF-8?q?mejorar=20notificaci=C3=B3n=20de=20errores?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/jobs/backtrace_job.rb | 16 ++++++++++++---- app/views/exception_notifier/_backtrace.text.erb | 3 +++ app/views/exception_notifier/_data.text.erb | 12 ++++++++++++ app/views/exception_notifier/_session.text.erb | 6 ++++++ config/environments/production.rb | 3 ++- config/initializers/filter_parameter_logging.rb | 2 +- 6 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 app/views/exception_notifier/_backtrace.text.erb create mode 100644 app/views/exception_notifier/_data.text.erb create mode 100644 app/views/exception_notifier/_session.text.erb diff --git a/app/jobs/backtrace_job.rb b/app/jobs/backtrace_job.rb index e99a3cee..1947bda6 100644 --- a/app/jobs/backtrace_job.rb +++ b/app/jobs/backtrace_job.rb @@ -2,6 +2,8 @@ # Procesa los errores de JavaScript class BacktraceJob < ApplicationJob + class BacktraceException < RuntimeError; end + queue_as :low_priority attr_reader :params, :site_id @@ -26,9 +28,9 @@ class BacktraceJob < ApplicationJob end begin - raise NoMethodError - rescue NoMethodError => e - ExceptionNotifier.notify_exception(e, data: { site: site.name, params: params }) + raise BacktraceException, params['errors']&.first&.dig('message') + rescue BacktraceException => e + ExceptionNotifier.notify_exception(e, data: { site: site.name, params: params, backtrace: true }) end end @@ -40,7 +42,13 @@ class BacktraceJob < ApplicationJob # Obtiene todos los archivos del backtrace def files - @files ||= params['errors'].map { |x| x['backtrace'] }.flatten.map { |x| x['file'].split('@').last }.uniq.select { |x| x&.start_with?(site.url) } + @files ||= params['errors'].map do |x| + x['backtrace'] + end.flatten.map do |x| + x['file'].split('@').last + end.uniq.select do |x| + x&.start_with?(site.url) + end end # Asume que todos los sourcemaps comparten la misma URL, lo diff --git a/app/views/exception_notifier/_backtrace.text.erb b/app/views/exception_notifier/_backtrace.text.erb new file mode 100644 index 00000000..9631be28 --- /dev/null +++ b/app/views/exception_notifier/_backtrace.text.erb @@ -0,0 +1,3 @@ +``` +<%= raw @backtrace.join("\n") %> +``` diff --git a/app/views/exception_notifier/_data.text.erb b/app/views/exception_notifier/_data.text.erb new file mode 100644 index 00000000..476fb4d9 --- /dev/null +++ b/app/views/exception_notifier/_data.text.erb @@ -0,0 +1,12 @@ +<% if @data['backtrace'] %> +<% @data['errors'].each do |error| %> +# <%= error['type'] %>: <%= error['message'] %> + +<%= Terminal::Table.new headings: error['backtrace'].first.keys, rows: error['backtrace'].map(&:values) %> + +<% end %> +<% else %> +```ruby +<%= raw PP.pp(@data, "") %> +``` +<% end %> diff --git a/app/views/exception_notifier/_session.text.erb b/app/views/exception_notifier/_session.text.erb new file mode 100644 index 00000000..28cf11ab --- /dev/null +++ b/app/views/exception_notifier/_session.text.erb @@ -0,0 +1,6 @@ +* session id: <%= @request.ssl? ? "[FILTERED]" : (raw (@request.session['session_id'] || (@request.env["rack.session.options"] and @request.env["rack.session.options"][:id])).inspect.html_safe) %> +* data: + +```ruby +<%= raw PP.pp(@request.session.to_hash, "") %> +``` diff --git a/config/environments/production.rb b/config/environments/production.rb index e166241d..e14ce497 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -151,7 +151,8 @@ Rails.application.configure do email: { email_prefix: '[ERROR] ', sender_address: ENV['DEFAULT_FROM'], - exception_recipients: ENV['EXCEPTION_TO'] + exception_recipients: ENV['EXCEPTION_TO'], + normalize_subject: true } Rails.application.routes.default_url_options[:host] = "panel.#{ENV.fetch('SUTTY', 'sutty.nl')}" diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb index 9f68e0dd..2fc446ff 100644 --- a/config/initializers/filter_parameter_logging.rb +++ b/config/initializers/filter_parameter_logging.rb @@ -4,5 +4,5 @@ # Configure sensitive parameters which will be filtered from the log file. Rails.application.config.filter_parameters += %i[ - password passw secret token _key crypt salt certificate otp ssn + password passw secret token _key crypt salt certificate otp ssn key ]