Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from cablett/cablett-everything-structured-log
Catch fatal errors and output as structured
- Loading branch information
Showing
6 changed files
with
101 additions
and
3 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
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
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,29 @@ | ||
|
||
module MailRoom | ||
class CrashHandler | ||
|
||
attr_reader :error, :format | ||
|
||
SUPPORTED_FORMATS = %w[json none] | ||
|
||
def initialize(error:, format:) | ||
@error = error | ||
@format = format | ||
end | ||
|
||
def handle | ||
if format == 'json' | ||
puts json | ||
return | ||
end | ||
|
||
raise error | ||
end | ||
|
||
private | ||
|
||
def json | ||
{ time: Time.now, severity: :fatal, message: error.message, backtrace: error.backtrace }.to_json | ||
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
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,41 @@ | ||
require 'spec_helper' | ||
|
||
describe MailRoom::CrashHandler do | ||
|
||
let(:error_message) { "oh noes!" } | ||
let(:error) { RuntimeError.new(error_message) } | ||
|
||
describe '#handle' do | ||
|
||
subject{ described_class.new(error: error, format: format) } | ||
|
||
context 'when given a json format' do | ||
let(:format) { 'json' } | ||
let(:fake_json) do | ||
{ message: error_message }.to_json | ||
end | ||
|
||
it 'outputs the result of json to stdout' do | ||
subject.stubs(:json).returns(fake_json) | ||
|
||
expect{ subject.handle }.to output(/\"message\":\"#{error_message}\"/).to_stdout | ||
end | ||
end | ||
|
||
context 'when given a blank format' do | ||
let(:format) { "" } | ||
|
||
it 'raises an error as designed' do | ||
expect{ subject.handle }.to raise_error(error.class, error_message) | ||
end | ||
end | ||
|
||
context 'when given a nonexistent format' do | ||
let(:format) { "nonsense" } | ||
|
||
it 'raises an error as designed' do | ||
expect{ subject.handle }.to raise_error(error.class, error_message) | ||
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