trouble 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2013 Bukowskis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Trouble
2
+
3
+ A unified wrapper to report errors and Exceptions.
4
+
5
+ This gem achieves independence of things like Bugsnag, Airbrake, etc.. If any of those is defined, Trouble will pass on the exception to them. Additionaly, everything is written to a custom logger you specify (defaults to `Rails.logger`).
6
+
7
+ Currently only Bugsnag is defined as a working backend.
8
+
9
+ # Usage
10
+
11
+ #### Syntax
12
+
13
+ ```ruby
14
+ Trouble.notify EXCEPTION, [METADATA-HASH]
15
+ ````
16
+
17
+ #### Examples
18
+
19
+ ```ruby
20
+ exception = RuntimeError.new
21
+
22
+ Trouble.notify exception
23
+ Trouble.notify exception, some_idea_why_it_happened: "I don't know, but try this and that."
24
+ ```
@@ -0,0 +1,43 @@
1
+ require 'logger'
2
+
3
+ module Trouble
4
+ class Configuration
5
+ attr_accessor :logger
6
+
7
+ def initialize(options={})
8
+ @logger = options[:logger] || default_logger
9
+ end
10
+
11
+ private
12
+
13
+ def default_logger
14
+ if defined?(Rails)
15
+ Rails.logger
16
+ else
17
+ Logger.new(STDOUT)
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+
24
+ module Trouble
25
+
26
+ # Public: Returns the the configuration instance.
27
+ #
28
+ def self.config
29
+ @config ||= Configuration.new
30
+ end
31
+
32
+ # Public: Yields the configuration instance.
33
+ #
34
+ def self.configure(&block)
35
+ yield config
36
+ end
37
+
38
+ # Public: Reset the configuration (useful for testing)
39
+ #
40
+ def self.reset!
41
+ @config = nil
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module Trouble
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 6
6
+
7
+ STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
+ end
9
+ end
data/lib/trouble.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'trouble/configuration'
2
+
3
+ # A generic wrapper to report errors and Exceptions.
4
+ #
5
+ module Trouble
6
+
7
+ # Public: Notify the developers about an Exception
8
+ #
9
+ # exception - An instance of an Exception
10
+ # metadata - An Hash with arbitrary additional information (optional)
11
+ #
12
+ # Examples
13
+ #
14
+ # Trouble.notify RuntimeError.new
15
+ # Trouble.notify RuntimeError.new, some_idea_why_it_happened: "I don't know, but try this and that."
16
+ #
17
+ # Returns nothing.
18
+ #
19
+ def self.notify(exception, metadata = nil)
20
+ exception.set_backtrace(caller) unless exception.backtrace
21
+ notify! exception, metadata
22
+ end
23
+
24
+ private
25
+
26
+ # Internal: Dispatch the Exception to the backend(s).
27
+ #
28
+ def self.notify!(exception, metadata)
29
+ log(exception, metadata) if config.logger
30
+ Bugsnag.notify(exception, metadata) if defined?(Bugsnag)
31
+ end
32
+
33
+ # Internal: Log to the current Logger.
34
+ #
35
+ def self.log(exception, metadata)
36
+ rows = ['TROUBLE NOTIFICATION']
37
+ rows << " | Exception: #{exception.inspect}"
38
+ rows << " | Metadata: #{metadata.inspect}"
39
+ rows << " \\ Location: #{exception.backtrace.first}\n"
40
+ config.logger.error rows.join("\n")
41
+ end
42
+
43
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ TroubleTestException = Class.new(RuntimeError)
4
+
5
+ describe Trouble do
6
+
7
+ let(:exception) { TroubleTestException.new('big problems') }
8
+ let(:metadata) { { details: 'mean bug'} }
9
+ let(:logger) { mock(:logger) }
10
+
11
+ let(:trouble) { Trouble }
12
+
13
+ describe '.notify' do
14
+ it 'does not cause any Exceptions if there is no Backend' do
15
+ trouble.notify(exception, metadata)
16
+ end
17
+
18
+ context 'with a Logger' do
19
+ before do
20
+ trouble.config.logger = mock(:logger)
21
+ end
22
+
23
+ it 'logs the output' do
24
+ trouble.config.logger .should_receive(:error) do |string|
25
+ string.should include('TROUBLE')
26
+ string.should include('TroubleTestException')
27
+ string.should include('big problems')
28
+ string.should include('mean bug')
29
+ string.should include('trouble_spec.rb')
30
+ end
31
+ trouble.notify exception, metadata
32
+ end
33
+ end
34
+
35
+ context 'with Bugsnag' do
36
+ before do
37
+ ensure_module :Bugsnag
38
+ Bugsnag.stub!(:notify)
39
+ end
40
+
41
+ describe '.notify' do
42
+ it 'uses Bugsnag as notification backend' do
43
+ Bugsnag.should_receive(:notify).with(exception, metadata)
44
+ trouble.notify exception, metadata
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '.logger' do
51
+ before do
52
+ Trouble.reset!
53
+ end
54
+
55
+ it 'is an STDOUT logger' do
56
+ Logger.should_receive(:new).with(STDOUT).and_return logger
57
+ trouble.config.logger.should be logger
58
+ end
59
+
60
+ context 'with Rails' do
61
+ before do
62
+ ensure_module :Rails
63
+ Rails.stub!(:logger).and_return(logger)
64
+ end
65
+
66
+ it 'is the Rails logger' do
67
+ trouble.config.logger.should be Rails.logger
68
+ end
69
+ end
70
+ end
71
+
72
+ end
@@ -0,0 +1,34 @@
1
+ require 'logger'
2
+ require 'trouble'
3
+
4
+ def ensure_class_or_module(full_name, class_or_module)
5
+ full_name.to_s.split(/::/).inject(Object) do |context, name|
6
+ begin
7
+ context.const_get(name)
8
+ rescue NameError
9
+ if class_or_module == :class
10
+ context.const_set(name, Class.new)
11
+ else
12
+ context.const_set(name, Module.new)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ def ensure_module(name)
19
+ ensure_class_or_module(name, :module)
20
+ end
21
+
22
+ def ensure_class(name)
23
+ ensure_class_or_module(name, :class)
24
+ end
25
+
26
+ RSpec.configure do |config|
27
+ config.before do
28
+ Trouble.config.logger = nil
29
+ end
30
+
31
+ config.after do
32
+ Trouble.reset!
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trouble
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - bukowskis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard-rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rb-fsevent
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: This gem achieves independence of things like Bugsnag, Airbrake, etc..
63
+ If any of those is defined, Trouble will pass on the exception to them.
64
+ email:
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/trouble/configuration.rb
70
+ - lib/trouble/version.rb
71
+ - lib/trouble.rb
72
+ - spec/lib/trouble_spec.rb
73
+ - spec/spec_helper.rb
74
+ - README.md
75
+ - LICENSE
76
+ homepage: https://github.com/bukowskis/trouble
77
+ licenses:
78
+ - MIT
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --encoding
82
+ - UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.23
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A generic abstraction layer for reporting errors and Exceptions.
103
+ test_files: []