kinetic_cafe_error 1.7 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e2c0e4bf3c54e900432944fabebcc48c1d8960b
4
- data.tar.gz: c54cb33eaf01dd5620e22c85a8b6a1f66c224b97
3
+ metadata.gz: dc17826f6f7567f2438621fac02064e64fae64ba
4
+ data.tar.gz: 8ee581bf23ad9bbedcb42638e01d159773f02c35
5
5
  SHA512:
6
- metadata.gz: 5451adec280190d619f7fc85ae9055aae91b5a0de05676e3395b586c225b71b4ac7e22a3e763c44b78f66449403b92bafd7cac0700038d336e9f5b3f669bbdde
7
- data.tar.gz: 38169cedbb31a0391c0f425d5c024eb5bd92aab4193bbd18aabe4b961a8a65181aa12b2072846cf2b9ece3a6343d9b001a76bdc1bc5c746a5b7fe06ff42e5f14
6
+ metadata.gz: 3398f8cefdabc226f3851a6b4ecf7e9a3dc0db0c135a66699341e71367ab53852745b09cf8856043ba79223a9e096c13ba83ff2405c008effe35979ae494e038
7
+ data.tar.gz: da175a041e62246c34e837723e0f631b9150f96891aec8271396464aaa3c09dcd456381aa689019bf9b17f04aa3b9b8110fe99874e21c9c143947fc60e1029f7
@@ -1,3 +1,14 @@
1
+ === 1.8.1 / 2015-10-26
2
+
3
+ * Re-release because 1.8 was yanked
4
+
5
+ === 1.8 / 2015-10-26
6
+
7
+ * 1 minor enhancement:
8
+
9
+ * Add +kinetic_cafe_error_handle_post_error+ to allow for capturing or
10
+ post-processing of errors after logging and handling.
11
+
1
12
  === 1.7 / 2015-08-05
2
13
 
3
14
  * 1 minor enhancement:
@@ -65,6 +65,19 @@ automatically injected, which enables the following functionality:
65
65
  that can be provided with the kinetic_cafe_error_handler_log_locale helper
66
66
  method.
67
67
 
68
+ The error can be then captured for processing by providing the
69
+ kinetic_cafe_error_handle_post_error method.
70
+
71
+ Example for capturing KineticCafe::Errors with raven-ruby for Sentry:
72
+
73
+ ExampleController < ActionController
74
+ include KineticCafe::ErrorHandler
75
+
76
+ def kinetic_cafe_error_handle_post_error(error)
77
+ Raven.capture_exception(error)
78
+ end
79
+ end
80
+
68
81
  === Using with Minitest
69
82
 
70
83
  KineticCafe::Error provides a number of assertions that can help testing that
@@ -110,7 +123,7 @@ KineticCafe::Error provides four experimental matchers:
110
123
 
111
124
  Add kinetic_cafe_error to your Gemfile:
112
125
 
113
- gem 'kinetic_cafe_error', '~> 1.7'
126
+ gem 'kinetic_cafe_error', '~> 1.8'
114
127
 
115
128
  If not using Rails, install with RubyGems:
116
129
 
data/Rakefile CHANGED
@@ -13,6 +13,7 @@ Hoe.plugin :email unless ENV['CI'] || ENV['TRAVIS']
13
13
 
14
14
  spec = Hoe.spec 'kinetic_cafe_error' do
15
15
  developer('Austin Ziegler', 'aziegler@kineticcafe.com')
16
+ developer('Jero Sutlovic', 'jsutlovic@kineticcafe.com')
16
17
 
17
18
  require_ruby_version '>= 1.9.2'
18
19
 
@@ -35,12 +35,19 @@ module KineticCafe::ErrorHandler
35
35
  # This method is called with +error+ when Rails catches a KineticCafe::Error
36
36
  # descendant. It logs the message and its cause as severity error. After
37
37
  # logging as +error+, it will render to HTML or JSON. The received error is
38
- # logged using the value of #kinetic_cafe_error_handler_log_locale.
38
+ # logged using the value of #kinetic_cafe_error_handler_log_locale. After
39
+ # rendering to HTML or JSON, +error+ will be passed to a post-processing
40
+ # handler.
39
41
  #
40
42
  # HTML is rendered with #kinetic_cafe_error_render_html. JSON is rendered
41
43
  # with #kinetic_cafe_error_render_json. Either of these can be overridden in
42
44
  # controllers for different behaviour.
43
45
  #
46
+ # The +error+ is passed to #kinetic_cafe_error_handle_post_error to be
47
+ # handled for post-processing. This should be overridden to implement
48
+ # post-processing, for example passing the error to an external error
49
+ # capturing/tracking service such as Airbrake or Sentry.
50
+ #
44
51
  # As an option, +kinetic_cafe_error_handler+ can also be used in a
45
52
  # +rescue_from+ block with an error class and parameters, and it will
46
53
  # construct the error for handling. The following example assumes that there
@@ -72,6 +79,8 @@ module KineticCafe::ErrorHandler
72
79
  kinetic_cafe_error_render_json(error)
73
80
  end
74
81
  end
82
+
83
+ kinetic_cafe_error_handle_post_error(error)
75
84
  end
76
85
 
77
86
  # Render the +error+ as HTML. Uses the template +kinetic_cafe_error/page+
@@ -101,16 +110,24 @@ module KineticCafe::ErrorHandler
101
110
  end
102
111
  end
103
112
 
113
+ def kinetic_cafe_error_handle_post_error(_error)
114
+ end
115
+
104
116
  # Write the provided error to the Rails log using the value of
105
117
  # #kinetic_cafe_error_handler_log_locale. If the error has a cause, log
106
- # that as well.
118
+ # that as well. Rails.logger.class is expected to have constants matching
119
+ # error.severity.upcase, which is used to determine the numeric severity
120
+ # to log the error at.
107
121
  def kinetic_cafe_error_log_error(error)
108
122
  locale = self.class.kinetic_cafe_error_handler_log_locale
109
- Rails.logger.error(error.message(locale))
123
+ severity = Rails.logger.class.const_get(error.severity.upcase)
124
+ Rails.logger.add(severity, nil, error.message(locale))
110
125
 
111
126
  return unless error.cause
112
127
 
113
- Rails.logger.error(
128
+ Rails.logger.add(
129
+ severity,
130
+ nil,
114
131
  t(
115
132
  'kinetic_cafe_error.cause',
116
133
  message: error.cause.message,
@@ -25,7 +25,7 @@ module KineticCafe # :nodoc:
25
25
  # rescue clause and handled there, as is shown in the included
26
26
  # KineticCafe::ErrorHandler controller concern for Rails.
27
27
  class Error < ::StandardError
28
- VERSION = '1.7' # :nodoc:
28
+ VERSION = '1.8.1' # :nodoc:
29
29
 
30
30
  # Get the KineticCafe::Error functionality.
31
31
  include KineticCafe::ErrorModule
@@ -176,6 +176,10 @@ module KineticCafe # :nodoc:
176
176
  defined?(Rack::Utils) && :bad_request || 400
177
177
  end
178
178
 
179
+ def default_severity
180
+ :error
181
+ end
182
+
179
183
  def stringify(object, namespace = nil)
180
184
  case object
181
185
  when Hash
@@ -53,22 +53,25 @@ module KineticCafe
53
53
  # Define a new error as a subclass of the exception hosting ErrorDSL.
54
54
  # Options is a Hash supporting the following values:
55
55
  #
56
- # +status+:: A number or Ruby symbol representing the HTTP status code
57
- # associated with this error. If not provided, defaults to
58
- # :bad_request. Must be provided if +class+ is provided. HTTP
59
- # status codes are defined in Rack::Utils.
60
- # +key+:: The name of the error class to be created. Provide as a
61
- # snake_case value that will be turned into a camelized class
62
- # name. Mutually exclusive with +class+.
63
- # +class+:: The name of the class the error is for. If present, +status+
64
- # must be provided to create a complete error class. That is,
56
+ # +status+:: A number or Ruby symbol representing the HTTP status code
57
+ # associated with this error. If not provided, defaults to
58
+ # :bad_request. Must be provided if +class+ is provided. HTTP
59
+ # status codes are defined in Rack::Utils.
60
+ # +severity+:: A Ruby symbol representing the severity level associated
61
+ # with this error. If not provided, defaults to :error. Error
62
+ # severity levels are defined in Logger::Severity.
63
+ # +key+:: The name of the error class to be created. Provide as a
64
+ # snake_case value that will be turned into a camelized class
65
+ # name. Mutually exclusive with +class+.
66
+ # +class+:: The name of the class the error is for. If present, +status+
67
+ # must be provided to create a complete error class. That is,
65
68
  #
66
- # define_error class: :object, status: :not_found
69
+ # define_error class: :object, status: :not_found
67
70
  #
68
- # will create an +ObjectNotFound+ error class.
69
- # +header+:: Indicates that when this is caught, it should not be returned
70
- # with full details, but should instead be treated as a
71
- # header-only API response. Also available as +header_only+.
71
+ # will create an +ObjectNotFound+ error class.
72
+ # +header+:: Indicates that when this is caught, it should not be
73
+ # returned with full details, but should instead be treated as
74
+ # a header-only API response. Also available as +header_only+.
72
75
  # +internal+:: Generates a response that indicates to clients that the
73
76
  # error should not be shown to users.
74
77
  # +i18n_params+:: An array of parameter names that are expected to be
@@ -80,6 +83,7 @@ module KineticCafe
80
83
 
81
84
  options = options.dup
82
85
  status = options[:status]
86
+ severity = options[:severity]
83
87
 
84
88
  klass = options.delete(:class)
85
89
  if klass
@@ -137,7 +141,11 @@ module KineticCafe
137
141
  status ||= defined?(Rack::Utils) && :bad_request || 400
138
142
  status.freeze
139
143
 
144
+ severity ||= :error
145
+ severity.freeze
146
+
140
147
  error.send :define_method, :default_status, -> { status } if status
148
+ error.send :define_method, :default_severity, -> { severity } if severity
141
149
  error.send :private, :default_status
142
150
 
143
151
  const_set(error_name, error)
@@ -6,6 +6,9 @@ module KineticCafe # :nodoc:
6
6
  # The HTTP status to be returned. If not provided in the constructor, uses
7
7
  # #default_status.
8
8
  attr_reader :status
9
+ # The severity to be returned. If not provided in the constructor, uses
10
+ # #default_severity
11
+ attr_reader :severity
9
12
  # Extra data relevant to recipients of the exception, provided on
10
13
  # construction.
11
14
  attr_reader :extra
@@ -39,6 +42,8 @@ module KineticCafe # :nodoc:
39
42
  # value.
40
43
  # +status+:: An override to the HTTP status code to be returned by this
41
44
  # error by default.
45
+ # +severity+:: An override to the default severity to be returned by this
46
+ # error.
42
47
  # +i18n_params+:: The parameters to be sent to I18n.translate with the
43
48
  # #i18n_key.
44
49
  # +cause+:: The exception that caused this error. Used to wrap an earlier
@@ -69,6 +74,7 @@ module KineticCafe # :nodoc:
69
74
  @message && @message.freeze
70
75
 
71
76
  @status = options.delete(:status) || default_status
77
+ @severity = options.delete(:severity) || default_severity
72
78
  @i18n_params = options.delete(:i18n_params) || {}
73
79
  @extra = options.delete(:extra)
74
80
 
@@ -147,6 +153,7 @@ module KineticCafe # :nodoc:
147
153
  {
148
154
  message: @message,
149
155
  status: status,
156
+ severity: severity,
150
157
  name: name,
151
158
  internal: internal?,
152
159
  i18n_message: i18n_message,
@@ -174,9 +181,9 @@ module KineticCafe # :nodoc:
174
181
  # Nice debugging version of a KineticCafe::Error
175
182
  def inspect
176
183
  "#<#{self.class}: name=#{name} status=#{status} " \
177
- "message=#{message.inspect} i18n_key=#{i18n_key} " \
178
- "i18n_params=#{@i18n_params.inspect} extra=#{extra.inspect} " \
179
- "cause=#{cause}>"
184
+ "severity=#{severity} message=#{message.inspect} " \
185
+ "i18n_key=#{i18n_key} i18n_params=#{@i18n_params.inspect} " \
186
+ "extra=#{extra.inspect} cause=#{cause}>"
180
187
  end
181
188
 
182
189
  private
@@ -7,7 +7,7 @@ describe KineticCafe::Error do
7
7
  it 'defaults correctly' do
8
8
  stub I18n, :translate, 'Untranslatable error' do
9
9
  expected = '#<KineticCafe::Error: name=error ' \
10
- "status=bad_request message=\"Untranslatable error\" " \
10
+ 'status=bad_request severity=error message="Untranslatable error" ' \
11
11
  'i18n_key=kcerrors.error i18n_params={} extra=nil cause=>'
12
12
  assert_equal expected, exception.inspect
13
13
  end
@@ -91,6 +91,7 @@ describe KineticCafe::Error do
91
91
  assert_equal(
92
92
  {
93
93
  status: :bad_request,
94
+ severity: :error,
94
95
  name: 'error',
95
96
  internal: false,
96
97
  i18n_key: 'kcerrors.error'
@@ -104,6 +105,7 @@ describe KineticCafe::Error do
104
105
  {
105
106
  error: {
106
107
  status: :bad_request,
108
+ severity: :error,
107
109
  name: 'error',
108
110
  internal: false,
109
111
  i18n_key: 'kcerrors.error'
@@ -121,6 +123,7 @@ describe KineticCafe::Error do
121
123
  json: {
122
124
  error: {
123
125
  status: :bad_request,
126
+ severity: :error,
124
127
  name: 'error',
125
128
  internal: false,
126
129
  i18n_key: 'kcerrors.error'
@@ -99,6 +99,11 @@ describe KineticCafe::ErrorDSL do
99
99
  assert_equal 400, instance.send(:default_status)
100
100
  end
101
101
  end
102
+
103
+ it 'returns :error for #default_severity (private)' do
104
+ refute base.private_instance_methods.include?(:default_severity)
105
+ assert_equal :error, instance.send(:default_severity)
106
+ end
102
107
  end
103
108
 
104
109
  describe 'class-based definition' do
@@ -138,6 +143,22 @@ describe KineticCafe::ErrorDSL do
138
143
  assert_equal 400, instance.send(:default_status)
139
144
  end
140
145
  end
146
+
147
+ it 'returns :error for #default_severity (private)' do
148
+ refute base.private_instance_methods.include?(:default_severity)
149
+ assert_equal :error, instance.send(:default_severity)
150
+ end
151
+ end
152
+
153
+ describe 'with severity' do
154
+ let(:child) {
155
+ base.define_error class: :child, severity: :fatal
156
+ }
157
+
158
+ it 'returns :fatal for #default_severity (private)' do
159
+ refute base.private_instance_methods.include?(:default_severity)
160
+ assert_equal :fatal, instance.send(:default_severity)
161
+ end
141
162
  end
142
163
 
143
164
  describe 'with symbol status' do
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinetic_cafe_error
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.7'
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Ziegler
8
+ - Jero Sutlovic
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2015-08-05 00:00:00.000000000 Z
12
+ date: 2015-10-26 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: minitest
@@ -16,14 +17,14 @@ dependencies:
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '5.7'
20
+ version: '5.8'
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '5.7'
27
+ version: '5.8'
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rdoc
29
30
  requirement: !ruby/object:Gem::Requirement
@@ -302,6 +303,7 @@ description: |-
302
303
  representations across both clients and servers.
303
304
  email:
304
305
  - aziegler@kineticcafe.com
306
+ - jsutlovic@kineticcafe.com
305
307
  executables: []
306
308
  extensions: []
307
309
  extra_rdoc_files: