From dde6154eae1e0d736d4fc4ef14734f2473dc0bed Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Thu, 6 Jun 2024 00:40:21 +0200 Subject: [PATCH 1/3] Introduce Rails common error handler --- lib/munster.rb | 1 + .../receive_webhooks_controller.rb | 4 ++-- lib/munster/templates/munster.rb | 23 ++++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/munster.rb b/lib/munster.rb index bfba678..a601512 100644 --- a/lib/munster.rb +++ b/lib/munster.rb @@ -20,4 +20,5 @@ class Munster::Configuration config_accessor(:processing_job_class) { Munster::ProcessingJob } config_accessor(:active_handlers) { [] } + config_accessor(:error_context) { {} } end diff --git a/lib/munster/controllers/receive_webhooks_controller.rb b/lib/munster/controllers/receive_webhooks_controller.rb index 5cb8b5f..1f40b83 100644 --- a/lib/munster/controllers/receive_webhooks_controller.rb +++ b/lib/munster/controllers/receive_webhooks_controller.rb @@ -19,8 +19,8 @@ def create rescue KeyError # handler was not found, so we return generic 404 error. render_error("Required parameters were not present in the request", :not_found) rescue => e - # TODO: add exception handler here - # Appsignal.add_exception(e) + Rails.error.set_context(Munster.configuration.error_context) + Rails.error.report(e) if handler&.expose_errors_to_sender? error_for_sender_from_exception(e) diff --git a/lib/munster/templates/munster.rb b/lib/munster/templates/munster.rb index bbade12..811f56e 100644 --- a/lib/munster/templates/munster.rb +++ b/lib/munster/templates/munster.rb @@ -3,5 +3,26 @@ # Example: # {:test => TestHandler, :inactive => InactiveHandler} config.active_handlers = {} - config.processing_job_class = Munster::ProcessingJob + + # It's possible to overwrite default processing job to enahance it. As example if you want to add proper locking or retry mechanism. + # + # Example: + # + # class WebhookProcessingJob < Munster::ProcessingJob + # def perform(webhook) + # TokenLock.with(name: "webhook-processing-#{webhook.id}") do + # super(webhook) + # end + # end + # + # This is how you can change processing job: + # + # config.processing_job_class = WebhookProcessingJob + + # We're using a common interface for error report provided by Rails, e.g Rails.error.report. In some cases + # you want to enhance those errors with additional context. As example to provide a namespace: + # + # { appsignal: { namespace: "webhooks" } } + # + # config.error_context = { appsignal: { namespace: "webhooks" } } end From 13490eaf0720b7a6e15a622be40b7b773794a3af Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Thu, 6 Jun 2024 00:47:24 +0200 Subject: [PATCH 2/3] add documentation --- CHANGELOG.md | 1 + README.md | 10 ++++++++++ lib/munster/controllers/receive_webhooks_controller.rb | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f66880e..48a7258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Handler methods are now defined as instance methods for simplicity. - Define service_id in initializer with active_handlers, instead of handler class. - Use ruby 3.0 as a base for standard/rubocop, format all code according to it. +- Introduce Rails common error reporter ( https://guides.rubyonrails.org/error_reporting.html ) ## 0.1.0 diff --git a/README.md b/README.md index 75d2ab5..aca9d3b 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,16 @@ This project depends on two dependencies: - Ruby >= 3.0 - Rails >= 7.0 +## Error reporter +This gem uses [Rails common error reporter](https://guides.rubyonrails.org/error_reporting.html) to report any possible error to services like Honeybadger, Appsignal, Sentry and etc. Most of those services already support this common interface, if not - it's not that hard to add this support on your own. + +It's possible to provide additional context for every error. e.g. +``` +Munster.configure do |config| + config.error_context = { appsignal: { namespace: "webhooks" } } +end +``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/lib/munster/controllers/receive_webhooks_controller.rb b/lib/munster/controllers/receive_webhooks_controller.rb index 1f40b83..fd2ad65 100644 --- a/lib/munster/controllers/receive_webhooks_controller.rb +++ b/lib/munster/controllers/receive_webhooks_controller.rb @@ -19,7 +19,7 @@ def create rescue KeyError # handler was not found, so we return generic 404 error. render_error("Required parameters were not present in the request", :not_found) rescue => e - Rails.error.set_context(Munster.configuration.error_context) + Rails.error.set_context(**Munster.configuration.error_context) Rails.error.report(e) if handler&.expose_errors_to_sender? From 5a5105e15d034c8995ffbfbd591fe9555675c004 Mon Sep 17 00:00:00 2001 From: "Stanislav (Stas) Katkov" Date: Thu, 6 Jun 2024 10:35:28 +0200 Subject: [PATCH 3/3] Update munster.rb --- lib/munster/templates/munster.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/munster/templates/munster.rb b/lib/munster/templates/munster.rb index 811f56e..df54433 100644 --- a/lib/munster/templates/munster.rb +++ b/lib/munster/templates/munster.rb @@ -19,7 +19,7 @@ # # config.processing_job_class = WebhookProcessingJob - # We're using a common interface for error report provided by Rails, e.g Rails.error.report. In some cases + # We're using a common interface for error reporting provided by Rails, e.g Rails.error.report. In some cases # you want to enhance those errors with additional context. As example to provide a namespace: # # { appsignal: { namespace: "webhooks" } }