Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrik/1fef41ccc9cba8eec9b0 to your computer and use it in GitHub Desktop.
Save henrik/1fef41ccc9cba8eec9b0 to your computer and use it in GitHub Desktop.
ExceptionNotifier class to notify devs about exceptions and unexpected situations via Honeybadger.
# Notify devs about an actual exception, or unexpected situation,
# without necessarily letting it appear to the user as a 500 error.
#
# Decouples the rest of the app from the specific exception logger service.
#
# Suggested location in a Rails project: lib/
class ExceptionNotifier
def self.notify(exception)
Honeybadger.notify(exception)
end
def self.message(message, details_or_context = nil, context_or_nothing = nil)
if context_or_nothing
details = details_or_context
context = context_or_nothing
elsif details_or_context.is_a?(Hash)
details = nil
context = details_or_context
else
details = details_or_context
context = {}
end
details ||= "(no message)"
Honeybadger.notify(
error_class: message,
error_message: details.to_s,
context: context.to_h,
)
end
end
require "spec_helper"
require "exception_notifier"
describe ExceptionNotifier, ".notify" do
before do
stub_const("Honeybadger", double)
end
it "passes an exception to Honeybadger" do
ex = StandardError.new("boom")
expect(Honeybadger).to receive(:notify).with(ex)
ExceptionNotifier.notify(ex)
end
end
describe ExceptionNotifier, ".message" do
before do
stub_const("Honeybadger", double)
end
it "passes a message to Honeybadger" do
expect(Honeybadger).to receive(:notify).with(
error_class: "Boom!",
error_message: "(no message)",
context: {},
)
ExceptionNotifier.message("Boom!")
expect(Honeybadger).to receive(:notify).with(
error_class: "Boom!",
error_message: "Details!",
context: {},
)
ExceptionNotifier.message("Boom!", "Details!")
expect(Honeybadger).to receive(:notify).with(
error_class: "Boom!",
error_message: "Details!",
context: { foo: "bar" },
)
ExceptionNotifier.message("Boom!", "Details!", foo: "bar")
expect(Honeybadger).to receive(:notify).with(
error_class: "Boom!",
error_message: "(no message)",
context: { foo: "bar" },
)
ExceptionNotifier.message("Boom!", foo: "bar")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment