-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom error handler for missing i18n translations
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module I18n | ||
class SentryExceptionHandler | ||
def call(exception, _locale, _key, _options) | ||
if Rails.env.production? && exception.is_a?(MissingTranslation) | ||
Sentry.capture_exception(exception) | ||
else | ||
raise exception.respond_to?(:to_exception) ? exception.to_exception : exception | ||
end | ||
end | ||
end | ||
end | ||
|
||
I18n.exception_handler = I18n::SentryExceptionHandler.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe I18n::SentryExceptionHandler do | ||
let(:key) { "missing.translation" } | ||
|
||
describe "#call" do | ||
it "raises exceptions in non-production environments" do | ||
expect { I18n.translate(key) }.to raise_exception(I18n::MissingTranslationData) | ||
end | ||
context "in production" do | ||
before do | ||
allow(Rails.env).to receive(:production?).and_return(true) | ||
allow(Sentry).to receive(:capture_exception) | ||
end | ||
it "captures the exception in Sentry" do | ||
I18n.translate(key) | ||
expect(Sentry).to have_received(:capture_exception).with( | ||
I18n::MissingTranslation.new(:en, key) | ||
) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters