Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Rails common error reporter #7

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions lib/munster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class Munster::Configuration

config_accessor(:processing_job_class) { Munster::ProcessingJob }
config_accessor(:active_handlers) { [] }
config_accessor(:error_context) { {} }
end
4 changes: 2 additions & 2 deletions lib/munster/controllers/receive_webhooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion lib/munster/templates/munster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 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" } }
#
# config.error_context = { appsignal: { namespace: "webhooks" } }
end