kinetic_cafe_error 1.10 → 1.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'active_support/concern'
2
3
 
3
4
  # A controller concern for KineticCafe::Error that rescues from
@@ -71,13 +72,17 @@ module KineticCafe::ErrorHandler
71
72
 
72
73
  kinetic_cafe_error_log_error(error)
73
74
 
74
- respond_to do |format|
75
- format.html do
76
- kinetic_cafe_error_render_html(error)
77
- end
78
- format.json do
79
- kinetic_cafe_error_render_json(error)
75
+ if respond_to?(:respond_to)
76
+ respond_to do |format|
77
+ format.html do
78
+ kinetic_cafe_error_render_html(error)
79
+ end
80
+ format.json do
81
+ kinetic_cafe_error_render_json(error)
82
+ end
80
83
  end
84
+ else
85
+ kinetic_cafe_error_render_json(error)
81
86
  end
82
87
 
83
88
  kinetic_cafe_error_handle_post_error(error)
@@ -88,7 +93,7 @@ module KineticCafe::ErrorHandler
88
93
  # to <tt>error.status</tt>.
89
94
  def kinetic_cafe_error_render_html(error)
90
95
  render template: 'kinetic_cafe_error/page', locals: { error: error },
91
- status: error.status
96
+ status: error.status
92
97
  end
93
98
 
94
99
  # Render the +error+ as JSON. If it is KineticCafe::Error#header_only?, only
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require_relative 'error_module'
2
3
 
3
4
  module KineticCafe # :nodoc:
@@ -25,7 +26,7 @@ module KineticCafe # :nodoc:
25
26
  # rescue clause and handled there, as is shown in the included
26
27
  # KineticCafe::ErrorHandler controller concern for Rails.
27
28
  class Error < ::StandardError
28
- VERSION = '1.10' # :nodoc:
29
+ VERSION = '1.11' # :nodoc:
29
30
 
30
31
  # Get the KineticCafe::Error functionality.
31
32
  include KineticCafe::ErrorModule
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Minitest #:nodoc:
2
3
  # Add assertions to Minitest for testing KineticCafe::Error objects.
3
4
  module KineticCafeErrorAssertions
@@ -20,8 +21,8 @@ module Minitest #:nodoc:
20
21
  actual = actual.dup
21
22
  actual.instance_variable_set(:@cause, nil)
22
23
  actual.instance_variable_set(:@initialized_cause, true)
23
- actual.instance_variable_get(:@i18n_params).tap do |params|
24
- params.delete(:cause)
24
+ actual.instance_variable_get(:@i18n_params).tap do |hash|
25
+ hash.delete(:cause)
25
26
  end
26
27
  end
27
28
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: false
1
2
  module KineticCafe
2
3
  # Make defining new children of KineticCafe::Error easy. Adds the
3
4
  # #define_error method.
@@ -17,7 +18,7 @@ module KineticCafe
17
18
  name.underscore.freeze
18
19
  else
19
20
  name.dup.tap { |n|
20
- n.gsub!(/[[:upper:]]/) { "_#$&".downcase }
21
+ n.gsub!(/[[:upper:]]/) do "_#{$&}".downcase end
21
22
  n.sub!(/^_/, '')
22
23
  }.freeze
23
24
  end
@@ -46,7 +47,7 @@ module KineticCafe
46
47
  if name.respond_to?(:camelize)
47
48
  name.camelize.freeze
48
49
  else
49
- "_#{name}".gsub(/_([a-z])/i) { $1.upcase }.freeze
50
+ "_#{name}".gsub(/_([a-z])/i) { Regexp.last_match(1).upcase }.freeze
50
51
  end
51
52
  end
52
53
 
@@ -87,9 +88,7 @@ module KineticCafe
87
88
 
88
89
  klass = options.delete(:class)
89
90
  if klass
90
- if options.key?(:key)
91
- fail ArgumentError, ":key conflicts with class:#{klass}"
92
- end
91
+ fail ArgumentError, ":key conflicts with class:#{klass}" if options.key?(:key)
93
92
 
94
93
  key = if status.kind_of?(Symbol) || status.kind_of?(String)
95
94
  "#{klass}_#{KineticCafe::ErrorDSL.namify(status)}"
@@ -112,8 +111,8 @@ module KineticCafe
112
111
 
113
112
  error_name = KineticCafe::ErrorDSL.camelize(key)
114
113
  i18n_key_base = respond_to?(:i18n_key_base) && self.i18n_key_base ||
115
- 'kcerrors'.freeze
116
- i18n_key = "#{i18n_key_base}.#{key}".freeze
114
+ 'kcerrors'
115
+ i18n_key = "#{i18n_key_base}.#{key}"
117
116
 
118
117
  if const_defined?(error_name)
119
118
  message = "key:#{key} already exists as #{error_name}"
@@ -152,8 +151,6 @@ module KineticCafe
152
151
  const_set(error_name, error)
153
152
  end
154
153
 
155
- private
156
-
157
154
  ##
158
155
  def self.included(_mod)
159
156
  fail "#{self} cannot be included"
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module KineticCafe
2
3
  # If Rails is defined, KineticCafe::ErrorEngine will be loaded automatically,
3
4
  # providing access to the KineticCafe::ErrorHandler.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module KineticCafe # :nodoc:
2
3
  # The core functionality provided by a KineticCafe::Error, extracted to a
3
4
  # module to ensure that exceptions that are made hosts of error hierarchies
@@ -83,7 +84,7 @@ module KineticCafe # :nodoc:
83
84
  initialize_cause(options.delete(:cause)) if options.key?(:cause)
84
85
 
85
86
  query = options.delete(:query)
86
- @i18n_params.merge!(query: stringify(query)) if query
87
+ @i18n_params[:query] = stringify(query) if query
87
88
  @i18n_params.merge!(options)
88
89
  end
89
90
 
@@ -103,19 +104,17 @@ module KineticCafe # :nodoc:
103
104
  @i18n_key ||= if self.class.respond_to? :i18n_key
104
105
  self.class.i18n_key
105
106
  else
106
- [
107
- i18n_key_base, (name)
108
- ].join('.').freeze
107
+ [ i18n_key_base, name ].join('.').freeze
109
108
  end
110
109
  end
111
- alias_method :code, :i18n_key
110
+ alias code i18n_key
112
111
 
113
112
  # Indicates that this error should *not* have its details rendered to the
114
113
  # user, but should use the +head+ method.
115
114
  def header?
116
115
  false
117
116
  end
118
- alias_method :header_only?, :header?
117
+ alias header_only? header?
119
118
 
120
119
  # Indicates that this error should be rendered to the client, but clients
121
120
  # are advised *not* to display the message to the user.
@@ -163,7 +162,7 @@ module KineticCafe # :nodoc:
163
162
  extra: extra
164
163
  }.delete_if { |_, v| v.nil? || (v.respond_to?(:empty?) && v.empty?) }
165
164
  end
166
- alias_method :as_json, :api_error
165
+ alias as_json api_error
167
166
 
168
167
  # An error result that can be passed as a response body.
169
168
  def error_result
@@ -176,7 +175,7 @@ module KineticCafe # :nodoc:
176
175
  def json_result
177
176
  { status: status, json: error_result, layout: false }
178
177
  end
179
- alias_method :render_json_for_rails, :json_result
178
+ alias render_json_for_rails json_result
180
179
 
181
180
  # Nice debugging version of a KineticCafe::Error
182
181
  def inspect
@@ -191,9 +190,7 @@ module KineticCafe # :nodoc:
191
190
  def initialize_cause(cause)
192
191
  return if cause.nil?
193
192
 
194
- unless cause.kind_of? Exception
195
- fail ArgumentError, 'cause must be an Exception'
196
- end
193
+ fail ArgumentError, 'cause must be an Exception' unless cause.kind_of? Exception
197
194
 
198
195
  @initialized_cause = true
199
196
  @cause = cause
@@ -212,7 +209,7 @@ module KineticCafe # :nodoc:
212
209
  end
213
210
 
214
211
  def stringify_hash(hash, namespace)
215
- hash.collect do |key, value|
212
+ hash.map do |key, value|
216
213
  key = namespace ? "#{namespace}[#{key}]" : key
217
214
  case value
218
215
  when Hash
@@ -231,7 +228,7 @@ module KineticCafe # :nodoc:
231
228
  if array.empty?
232
229
  stringify_value(key, [])
233
230
  else
234
- array.collect { |value| stringify(value, key) }.join(', ')
231
+ array.map { |value| stringify(value, key) }.join(', ')
235
232
  end
236
233
  end
237
234
 
@@ -268,7 +265,7 @@ module KineticCafe # :nodoc:
268
265
  ##
269
266
  def included(mod)
270
267
  default_singleton_method mod, :i18n_key_base do
271
- 'kcerrors'.freeze
268
+ 'kcerrors'
272
269
  end
273
270
 
274
271
  default_singleton_method mod, :i18n_params do
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'rspec/expectations'
2
3
 
3
4
  module KineticCafe
@@ -45,8 +46,8 @@ module KineticCafe
45
46
  actual = actual.dup
46
47
  actual.instance_variable_set(:@cause, nil)
47
48
  actual.instance_variable_set(:@initialized_cause, true)
48
- actual.instance_variable_get(:@i18n_params).tap do |params|
49
- params.delete(:cause)
49
+ actual.instance_variable_get(:@i18n_params).tap do |hash|
50
+ hash.delete(:cause)
50
51
  end
51
52
  end
52
53
 
@@ -1,22 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module KineticCafe
2
- module ErrorTasks
4
+ # Tasks to assist with managing kinetic_cafe_error classes in Rake.
5
+ module ErrorTasks #:nodoc:
3
6
  extend Rake::DSL
4
7
  end
5
8
  end
6
9
 
7
- module KineticCafe::ErrorTasks
10
+ ##
11
+ module KineticCafe::ErrorTasks #:nodoc:
8
12
  require 'kinetic_cafe_error'
9
13
 
10
14
  namespace :kcerror do
11
15
  desc 'Show defined errors.'
12
16
  task :defined, [ :params ] => 'kcerror:find' do |_, args|
13
- show_params = args.params =~ /^y/i
14
-
15
17
  display = ->(root, prefix: '', show_params: false) {
16
18
  line = "#{prefix}- #{root}"
17
19
 
18
20
  if !root.i18n_params.empty? && show_params
19
- line << " (" << root.i18n_params.join(', ') << ")"
21
+ line << ' (' << root.i18n_params.join(', ') << ')'
20
22
  end
21
23
 
22
24
  puts line
@@ -34,6 +36,8 @@ module KineticCafe::ErrorTasks
34
36
  end
35
37
  }
36
38
 
39
+ show_params = args.params =~ /^y/i
40
+
37
41
  if @descendants[StandardError]
38
42
  @descendants[StandardError].sort_by(&:to_s).each { |d|
39
43
  display.(d, show_params: show_params)
@@ -53,9 +57,9 @@ module KineticCafe::ErrorTasks
53
57
  params = root.i18n_params.map { |param| "%{#{param}}" }.join(' ')
54
58
 
55
59
  if params.empty?
56
- translation[name] = %Q(Translation for #{name} with no params.)
60
+ translation[name] = "Translation for #{name} with no params."
57
61
  else
58
- translation[name] = %Q(Translation for #{name} with #{params}.)
62
+ translation[name] = "Translation for #{name} with #{params}."
59
63
  end
60
64
 
61
65
  if @descendants[root]
@@ -64,19 +68,19 @@ module KineticCafe::ErrorTasks
64
68
  }
65
69
 
66
70
  if @descendants[StandardError]
67
- @descendants[StandardError].sort_by(&:to_s).each { |d| traverse.(d) }
71
+ @descendants[StandardError].sort_by(&:to_s).each do |d| traverse.(d) end
68
72
 
69
73
  # Normal YAML dump does not match the pattern that we want to compare
70
74
  # against. Therefore, we are going to write this file manually.
71
75
  text = 'kc:'
72
76
 
73
- translations.keys.sort.each { |group|
77
+ translations.keys.sort.each do |group|
74
78
  text << "\n #{group}:"
75
79
 
76
80
  translations[group].keys.sort.each { |k|
77
81
  text << "\n #{k}: >-\n #{translations[group][k]}"
78
82
  }
79
- }
83
+ end
80
84
 
81
85
  if args.output
82
86
  File.open(args.output, 'w') { |f| f.puts text }
@@ -1 +1,2 @@
1
+ # frozen_string_literal: true
1
2
  require 'kinetic_cafe/error'
@@ -1,4 +1,5 @@
1
1
  # -*- ruby encoding: utf-8 -*-
2
+ # frozen_string_literal: true
2
3
 
3
4
  gem 'minitest'
4
5
  require 'minitest/autorun'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  describe KineticCafe::Error do
@@ -156,8 +157,7 @@ describe KineticCafe::Error do
156
157
  fail 'causing'
157
158
  rescue => ex
158
159
  @causing_exception = ex
159
- raise KineticCafe::Error, cause: @causing_exception,
160
- message: 'wrapping'
160
+ raise KineticCafe::Error, cause: @causing_exception, message: 'wrapping'
161
161
  end
162
162
  rescue => ex
163
163
  @wrapping_exception = ex
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
  require 'rack/test'
3
4
 
@@ -262,9 +263,9 @@ describe KineticCafe::ErrorDSL do
262
263
 
263
264
  describe 'when Rack::Utils is defined' do
264
265
  it 'defines .not_found by default' do
265
- base = Class.new(StandardError) do
266
+ base = Class.new(StandardError) {
266
267
  extend KineticCafe::ErrorDSL
267
- end
268
+ }
268
269
  assert base.respond_to?(:not_found)
269
270
 
270
271
  child = base.not_found class: :child
@@ -272,20 +273,20 @@ describe KineticCafe::ErrorDSL do
272
273
  end
273
274
 
274
275
  it 'defines NotFound by default' do
275
- base = Class.new(StandardError) do
276
+ base = Class.new(StandardError) {
276
277
  extend KineticCafe::ErrorDSL
277
- end
278
+ }
278
279
  assert base.const_defined?(:NotFound)
279
280
  end
280
281
 
281
282
  it 'respects the method __rack_status if defined' do
282
- base = Class.new(StandardError) do
283
+ base = Class.new(StandardError) {
283
284
  def self.__rack_status
284
285
  { methods: true, errors: false }
285
286
  end
286
287
 
287
288
  extend KineticCafe::ErrorDSL
288
- end
289
+ }
289
290
 
290
291
  assert base.respond_to?(:not_found)
291
292
  refute base.const_defined?(:NotFound)
@@ -294,14 +295,13 @@ describe KineticCafe::ErrorDSL do
294
295
  it 'protects against badly named Rack status symbols' do
295
296
  begin
296
297
  Rack::Utils::SYMBOL_TO_STATUS_CODE[:"another_(silly)_love_song"] = 999
297
- base = Class.new(StandardError) do
298
+ base = Class.new(StandardError) {
298
299
  extend KineticCafe::ErrorDSL
299
- end
300
+ }
300
301
  assert base.respond_to?(:another_silly_love_song)
301
302
  assert base.const_defined?(:AnotherSillyLoveSong)
302
303
  ensure
303
304
  Rack::Utils::SYMBOL_TO_STATUS_CODE.delete(:"another_(silly)_love_song")
304
- $debugme = false
305
305
  end
306
306
  end
307
307
  end
@@ -309,9 +309,9 @@ describe KineticCafe::ErrorDSL do
309
309
  describe 'when Rack::Utils is not defined' do
310
310
  it 'does not define .not_found' do
311
311
  Rack.stub_remove_const(:Utils) do
312
- base = Class.new(StandardError) do
312
+ base = Class.new(StandardError) {
313
313
  extend KineticCafe::ErrorDSL
314
- end
314
+ }
315
315
 
316
316
  refute base.respond_to?(:not_found)
317
317
  end
@@ -319,9 +319,9 @@ describe KineticCafe::ErrorDSL do
319
319
 
320
320
  it 'does not define NotFound' do
321
321
  Rack.stub_remove_const(:Utils) do
322
- base = Class.new(StandardError) do
322
+ base = Class.new(StandardError) {
323
323
  extend KineticCafe::ErrorDSL
324
- end
324
+ }
325
325
 
326
326
  refute base.const_defined?(:NotFound)
327
327
  end
@@ -329,13 +329,13 @@ describe KineticCafe::ErrorDSL do
329
329
 
330
330
  it 'ignores __rack_status if defined' do
331
331
  Rack.stub_remove_const(:Utils) do
332
- base = Class.new(StandardError) do
332
+ base = Class.new(StandardError) {
333
333
  def self.__rack_status
334
334
  { methods: true, errors: false }
335
335
  end
336
336
 
337
337
  extend KineticCafe::ErrorDSL
338
- end
338
+ }
339
339
 
340
340
  refute base.respond_to?(:not_found)
341
341
  refute base.const_defined?(:NotFound)
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
  require 'rack/test'
3
4
 
@@ -92,7 +93,7 @@ describe KineticCafe::Error, '.hierarchy' do
92
93
  end
93
94
 
94
95
  it 'can handle a query parameter (issue #9)' do
95
- expected = { query: "id: 1" }
96
+ expected = { query: 'id: 1' }
96
97
  actual = My.new(query: { id: 1 }).instance_variable_get(:@i18n_params)
97
98
  assert_equal expected, actual
98
99
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinetic_cafe_error
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.10'
4
+ version: '1.11'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Ziegler
8
8
  - Jero Sutlovic
9
+ - Ravi Desai
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2016-03-25 00:00:00.000000000 Z
13
+ date: 2016-05-24 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: minitest
@@ -17,14 +18,14 @@ dependencies:
17
18
  requirements:
18
19
  - - "~>"
19
20
  - !ruby/object:Gem::Version
20
- version: '5.8'
21
+ version: '5.9'
21
22
  type: :development
22
23
  prerelease: false
23
24
  version_requirements: !ruby/object:Gem::Requirement
24
25
  requirements:
25
26
  - - "~>"
26
27
  - !ruby/object:Gem::Version
27
- version: '5.8'
28
+ version: '5.9'
28
29
  - !ruby/object:Gem::Dependency
29
30
  name: rdoc
30
31
  requirement: !ruby/object:Gem::Requirement
@@ -211,16 +212,22 @@ dependencies:
211
212
  name: rake
212
213
  requirement: !ruby/object:Gem::Requirement
213
214
  requirements:
214
- - - "~>"
215
+ - - ">="
215
216
  - !ruby/object:Gem::Version
216
217
  version: '10.0'
218
+ - - "<"
219
+ - !ruby/object:Gem::Version
220
+ version: '12'
217
221
  type: :development
218
222
  prerelease: false
219
223
  version_requirements: !ruby/object:Gem::Requirement
220
224
  requirements:
221
- - - "~>"
225
+ - - ">="
222
226
  - !ruby/object:Gem::Version
223
227
  version: '10.0'
228
+ - - "<"
229
+ - !ruby/object:Gem::Version
230
+ version: '12'
224
231
  - !ruby/object:Gem::Dependency
225
232
  name: i18n-tasks
226
233
  requirement: !ruby/object:Gem::Requirement
@@ -297,14 +304,14 @@ dependencies:
297
304
  requirements:
298
305
  - - "~>"
299
306
  - !ruby/object:Gem::Version
300
- version: '3.14'
307
+ version: '3.15'
301
308
  type: :development
302
309
  prerelease: false
303
310
  version_requirements: !ruby/object:Gem::Requirement
304
311
  requirements:
305
312
  - - "~>"
306
313
  - !ruby/object:Gem::Version
307
- version: '3.14'
314
+ version: '3.15'
308
315
  description: |-
309
316
  kinetic_cafe_error provides an API-smart error base class and a DSL for
310
317
  defining errors. Under Rails, it also provides a controller concern
@@ -318,24 +325,19 @@ description: |-
318
325
  email:
319
326
  - aziegler@kineticcafe.com
320
327
  - jsutlovic@kineticcafe.com
328
+ - rdesai@kineticcafe.com
321
329
  executables: []
322
330
  extensions: []
323
331
  extra_rdoc_files:
324
- - Contributing.rdoc
325
- - History.rdoc
326
- - Licence.rdoc
332
+ - Contributing.md
333
+ - History.md
334
+ - Licence.md
327
335
  - Manifest.txt
328
336
  - README.rdoc
329
337
  files:
330
- - ".autotest"
331
- - ".gemtest"
332
- - ".rubocop.yml"
333
- - ".travis.yml"
334
- - Appraisals
335
- - Contributing.rdoc
336
- - Gemfile
337
- - History.rdoc
338
- - Licence.rdoc
338
+ - Contributing.md
339
+ - History.md
340
+ - Licence.md
339
341
  - Manifest.txt
340
342
  - README.rdoc
341
343
  - Rakefile
@@ -353,8 +355,6 @@ files:
353
355
  - config/locales/kinetic_cafe_error.en.yml
354
356
  - config/locales/kinetic_cafe_error.fr-CA.yml
355
357
  - config/locales/kinetic_cafe_error.fr.yml
356
- - gemfiles/rack_1.6.gemfile
357
- - gemfiles/rack_2.gemfile
358
358
  - lib/kinetic_cafe/error.rb
359
359
  - lib/kinetic_cafe/error/minitest.rb
360
360
  - lib/kinetic_cafe/error_dsl.rb
@@ -380,7 +380,7 @@ require_paths:
380
380
  - lib
381
381
  required_ruby_version: !ruby/object:Gem::Requirement
382
382
  requirements:
383
- - - ">="
383
+ - - "~>"
384
384
  - !ruby/object:Gem::Version
385
385
  version: '2.1'
386
386
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -390,7 +390,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
390
390
  version: '0'
391
391
  requirements: []
392
392
  rubyforge_project:
393
- rubygems_version: 2.5.1
393
+ rubygems_version: 2.6.4
394
394
  signing_key:
395
395
  specification_version: 4
396
396
  summary: kinetic_cafe_error provides an API-smart error base class and a DSL for defining