kinetic_cafe_error 1.2 → 1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f729db0dc8d7bd5d7299f904a9731348d5d22d0
4
- data.tar.gz: 5f2cca7f8b42a6cfb4cc6cb45b6f4bc8602ac762
3
+ metadata.gz: 210985e1268b5a3d32fb0a46c69d854eb5523073
4
+ data.tar.gz: 0b2185f4a90efb887bf48af8a435646a79fe1900
5
5
  SHA512:
6
- metadata.gz: 2172f4681a2163199a49c1a099d84a50d6853db13db34e3462df7944239d8922c4e4e25c066ad170d8b82b4331d8ad9a33af00aae8da9b31a6b1e4449d84bb82
7
- data.tar.gz: a134be803972f943ee5189ac97530f15c5bda8756b146d2c3996d4e89f99a9e72bd34733dc8bdc2a0860b1c92a78619d8064a4767d1859648d330dbbf8f0ea59
6
+ metadata.gz: 8627e47d35629f7a6c8c6b58bb71036cf5cb0c2474cc752e4c033c3d8c678b2c22029ccc80552f5e98688f1e131863481d0e1391257d2f9f1a084e3e940639c4
7
+ data.tar.gz: 4e0d19ba153eb3d6f3ee7367ad6eeaecac5ac9abf0e7e26b3ae11db16e00042603c0bedcbcadbe10edba9fd99fa449fd0198a307062fe7d0ad60fc511ecd00ac
@@ -1,3 +1,29 @@
1
+ === 1.3 / 2015-06-18
2
+
3
+ * 3 minor enhancements
4
+
5
+ * Added a controller method, kinetic_cafe_error_handler_log_error, in
6
+ KineticCafe::ErrorHandler that will log the given error in the default
7
+ logging language, and its cause, if any.
8
+
9
+ * Added a controller class method to change the default logging language for
10
+ kinetic_cafe_error_handler_log_error. A locale may be provided with
11
+ kinetic_cafe_error_handler_log_locale. Fixes
12
+ [#2]{https://github.com/KineticCafe/kinetic_cafe_error/issues/2}.
13
+
14
+ * Added an optional +locale+ parameter to KineticCafe::ErrorModule#message
15
+ and KineticCafe::ErrorModule::#i18n_message. If I18n is present and
16
+ +locale+ is provided, the translation will *not* be cached and the
17
+ translation will be performed using the provided locale.
18
+
19
+ * 1 bug fixed
20
+
21
+ * The Minitest assertion, assert_kc_error, could not work because it was
22
+ looking for descendants of KineticCafe::ErrorDSL, not
23
+ KineticCafe::ErrorModule. Reported by @richardsondx as
24
+ [#3]{https://github.com/KineticCafe/kinetic_cafe_error/issues/3} during a
25
+ pairing session.
26
+
1
27
  === 1.2 / 2015-06-08
2
28
 
3
29
  * 1 major enhancement
@@ -60,6 +60,10 @@ automatically injected, which enables the following functionality:
60
60
 
61
61
  #kinetic_cafe_error_handler distinguishes between HTML and JSON contexts.
62
62
 
63
+ The error will be logged in a single language, with a configuration option
64
+ that can be provided with the kinetic_cafe_error_handler_log_locale helper
65
+ method.
66
+
63
67
  === Using with Minitest
64
68
 
65
69
  KineticCafe::Error provides a number of assertions that can help testing that
data/Rakefile CHANGED
@@ -32,6 +32,7 @@ spec = Hoe.spec "kinetic_cafe_error" do
32
32
  extra_dev_deps << ['hoe-travis', '~> 1.2']
33
33
  extra_dev_deps << ['minitest', '~> 5.4']
34
34
  extra_dev_deps << ['minitest-autotest', '~> 1.0']
35
+ extra_dev_deps << ['minitest-bonus-assertions', '~> 1.0']
35
36
  extra_dev_deps << ['minitest-focus', '~> 1.1']
36
37
  extra_dev_deps << ['minitest-moar', '~> 0.0']
37
38
  extra_dev_deps << ['minitest-stub-const', '~> 0.4']
@@ -17,20 +17,29 @@ module KineticCafe::ErrorHandler
17
17
  def kinetic_cafe_error_handler_for(klass)
18
18
  rescue_from klass, with: :kinetic_cafe_error_handler
19
19
  end
20
+
21
+ # Logging should be done in a single language, not many languages. By
22
+ # default, KineticCafe::Error will log errors received in the locale
23
+ # specified by I18n.default_locale. This method can be used to change the
24
+ # common logging locale for KineticCafe::Error handling without changing
25
+ # I18n.default_locale.
26
+ def kinetic_cafe_error_handler_log_locale(locale = nil)
27
+ @@kinetic_cafe_error_handler_log_locale = locale if locale
28
+ @@kinetic_cafe_error_handler_log_locale ||= I18n.default_locale
29
+ @@kinetic_cafe_error_handler_log_locale
30
+ end
20
31
  end
21
32
 
22
33
  # This method is called with +error+ when Rails catches a KineticCafe::Error
23
34
  # descendant. It logs the message and its cause as severity error. After
24
- # logging, it will render to HTML or JSON.
35
+ # logging as +error+, it will render to HTML or JSON. The received error is
36
+ # logged using the value of #kinetic_cafe_error_handler_log_locale.
25
37
  #
26
38
  # HTML is rendered with #kinetic_cafe_error_render_html. JSON is rendered
27
39
  # with #kinetic_cafe_error_render_json. Either of these can be overridden in
28
40
  # controllers for different behaviour.
29
41
  def kinetic_cafe_error_handler(error)
30
- Rails.logger.error(error.message)
31
- if error.cause
32
- Rails.logger.error(t('kinetic_cafe_error.cause', error.cause.message))
33
- end
42
+ kinetic_cafe_error_log_error(error)
34
43
 
35
44
  respond_to do |format|
36
45
  format.html do
@@ -68,4 +77,21 @@ module KineticCafe::ErrorHandler
68
77
  render error.json_result
69
78
  end
70
79
  end
80
+
81
+ # Write the provided error to the Rails log using the value of
82
+ # #kinetic_cafe_error_handler_log_locale. If the error has a cause, log
83
+ # that as well.
84
+ def kinetic_cafe_error_log_error(error)
85
+ locale = self.class.kinetic_cafe_error_handler_log_locale
86
+ Rails.logger.error(error.message(locale))
87
+ if error.cause
88
+ Rails.logger.error(
89
+ t(
90
+ 'kinetic_cafe_error.cause',
91
+ message: error.cause.message,
92
+ locale: locale
93
+ )
94
+ )
95
+ end
96
+ end
71
97
  end
@@ -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.2' # :nodoc:
28
+ VERSION = '1.3' # :nodoc:
29
29
 
30
30
  # Get the KineticCafe::Error functionality.
31
31
  include KineticCafe::ErrorModule
@@ -7,7 +7,7 @@ module Minitest #:nodoc:
7
7
  def assert_kc_error expected, actual, params = {}, msg = nil
8
8
  msg, params = params, {} if msg.nil? && params.kind_of?(String)
9
9
 
10
- assert_kind_of KineticCafe::ErrorDSL, actual,
10
+ assert_kind_of KineticCafe::ErrorModule, actual,
11
11
  msg || "Expected #{actual} to be #{expected}, but it was not."
12
12
 
13
13
  assert_kind_of expected, actual,
@@ -64,9 +64,9 @@ module KineticCafe # :nodoc:
64
64
  end
65
65
 
66
66
  # The message associated with this exception. If not provided, defaults to
67
- # #i18n_message.
68
- def message
69
- @message || i18n_message
67
+ # #i18n_message, which is passed the optional +locale+.
68
+ def message(locale = nil)
69
+ @message || i18n_message(locale)
70
70
  end
71
71
 
72
72
  # The name of the error class.
@@ -99,14 +99,28 @@ module KineticCafe # :nodoc:
99
99
  false
100
100
  end
101
101
 
102
- # The I18n translation of the message. If I18n.translate is defined,
103
- # returns #i18n_key and the I18n parameters.
104
- def i18n_message
105
- @i18n_message ||= if defined?(I18n.translate)
106
- I18n.translate(i18n_key, @i18n_params).freeze
107
- else
108
- [ i18n_key, @i18n_params ].freeze
109
- end
102
+ # The I18n translation of the message. If I18n.translate is not defined,
103
+ # returns #i18n_key and the I18n parameters as an array.
104
+ #
105
+ # If I18n is provided, the translation will be performed using the default
106
+ # locale. The message will be cached unless +locale+ is provided, which
107
+ # selects a specific locale for translation.
108
+ #
109
+ # +locale+ may be provided as a bare locale (<tt>:en</tt>) or as a hash
110
+ # value (<tt>locale: :en</tt>).
111
+ def i18n_message(locale = nil)
112
+ if defined?(I18n.translate)
113
+ case locale
114
+ when Hash
115
+ I18n.translate(i18n_key, @i18n_params.merge(locale))
116
+ when nil
117
+ @i18n_message ||= I18n.translate(i18n_key, @i18n_params).freeze
118
+ else
119
+ I18n.translate(i18n_key, @i18n_params.merge(locale: locale))
120
+ end
121
+ else
122
+ @i18n_message ||= [ i18n_key, @i18n_params ].freeze
123
+ end
110
124
  end
111
125
 
112
126
  # The details of this error as a hash. Values that are empty or nil are
@@ -5,6 +5,7 @@ require 'minitest/autorun'
5
5
  require 'minitest/focus'
6
6
  require 'minitest/moar'
7
7
  require 'minitest/stub_const'
8
+ require 'minitest-bonus-assertions'
8
9
  require 'rack/test'
9
10
  require 'kinetic_cafe_error'
10
11
 
@@ -13,9 +13,77 @@ describe KineticCafe::Error do
13
13
  end
14
14
  end
15
15
 
16
- it 'returns key/params if I18n.translate is not defined' do
17
- Object.stub_remove_const(:I18n) do
18
- assert_equal [ 'kcerrors.error', {} ], exception.i18n_message
16
+ describe '#messsage and #i18n_message' do
17
+ it 'returns key/params if I18n.translate is not defined' do
18
+ Object.stub_remove_const(:I18n) do
19
+ assert_equal [ 'kcerrors.error', {} ], exception.i18n_message
20
+ end
21
+ end
22
+
23
+ it 'encodes the :query parameter specially for I18n parameters' do
24
+ query = {
25
+ a: 1,
26
+ b: %w(x y z),
27
+ c: [ d: 1, e: 2 ],
28
+ f: []
29
+ }
30
+
31
+ exception = KineticCafe::Error.new(query: query)
32
+ Object.stub_remove_const(:I18n) do
33
+ assert_equal(
34
+ [
35
+ 'kcerrors.error',
36
+ { query: "a: 1; b[]: x, b[]: y, b[]: z; c[][d]: 1; c[][e]: 2; f[]: []" }
37
+ ],
38
+ exception.i18n_message
39
+ )
40
+ end
41
+ end
42
+
43
+ it 'calls I18n.translate if defined' do
44
+ matcher = ->(key, options) {
45
+ assert_equal 'kcerrors.error', key
46
+ assert_kind_of Hash, options
47
+ assert_missing_keys options, :locale
48
+ }
49
+
50
+ stub I18n, :translate, matcher do
51
+ exception.i18n_message
52
+ end
53
+ end
54
+
55
+ it 'calls I18n.translate with a bare locale if given one' do
56
+ matcher = ->(key, options) {
57
+ assert_equal 'kcerrors.error', key
58
+ assert_kind_of Hash, options
59
+ assert_has_keys options, :locale
60
+ assert_equal :kc, options[:locale]
61
+ }
62
+
63
+ stub I18n, :translate, matcher do
64
+ exception.i18n_message(:kc)
65
+ end
66
+ end
67
+
68
+ it 'calls I18n.translate with a hash locale if given one' do
69
+ matcher = ->(key, options) {
70
+ assert_equal 'kcerrors.error', key
71
+ assert_kind_of Hash, options
72
+ assert_has_keys options, :locale
73
+ assert_equal :kc, options[:locale]
74
+ }
75
+
76
+ stub I18n, :translate, matcher do
77
+ exception.i18n_message(locale: :kc)
78
+ end
79
+ end
80
+
81
+ it '#message fowards to #i18n_message if no @message' do
82
+ stub exception, :i18n_message do
83
+ exception.message
84
+ end
85
+
86
+ assert_instance_called exception, :i18n_message
19
87
  end
20
88
  end
21
89
 
@@ -65,26 +133,6 @@ describe KineticCafe::Error do
65
133
  )
66
134
  end
67
135
 
68
- it 'encodes the :query parameter specially for I18n parameters' do
69
- query = {
70
- a: 1,
71
- b: %w(x y z),
72
- c: [ d: 1, e: 2 ],
73
- f: []
74
- }
75
-
76
- exception = KineticCafe::Error.new(query: query)
77
- Object.stub_remove_const(:I18n) do
78
- assert_equal(
79
- [
80
- 'kcerrors.error',
81
- { query: "a: 1; b[]: x, b[]: y, b[]: z; c[][d]: 1; c[][e]: 2; f[]: []" }
82
- ],
83
- exception.i18n_message
84
- )
85
- end
86
- end
87
-
88
136
  it 'is not #header? by default' do
89
137
  refute exception.header?
90
138
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinetic_cafe_error
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Ziegler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-08 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: minitest-bonus-assertions
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: minitest-focus
127
141
  requirement: !ruby/object:Gem::Requirement