errawr 1.0.0 → 1.1.0

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: 3f191ece7e68880cb8fe7c0f1b7602b93ec7682b
4
- data.tar.gz: af5f3f544c7424e14b3a82b15b5767937b55208d
3
+ metadata.gz: a3b043ffb1dcda46e9beb5498e5978b54adf09fc
4
+ data.tar.gz: c5528e95eb0ce52255fb7f92a77f48efac869686
5
5
  SHA512:
6
- metadata.gz: e13c00a643cf2b978bb901fcac57b8f9a5c49d55ce25519d445b347c1214c8e472e06fae443a53000c9f4b6d88c960e304912f4f70f7114501dd0c5631a8a3ea
7
- data.tar.gz: 2d963cad145fd66659e42f929021277cb20e7f25dc6919985ec22ec011cb40c3bbf0b25635fe46ec458f29594661bc5b8420bf761e01c4b5d913269f1f174dd6
6
+ metadata.gz: a1fa4e75ee111570f0de9ab27778823c0e1c5e746c5c7b765819d6341f025c863d5b0715be5f01bb7ca8fdee0908161c420fa8a49e18841c2eb593c0e1d940b3
7
+ data.tar.gz: 1cb2fe1943cc8ec4d792fe5047a06bbc5cb91e735a7441d661710098c3b7cc43ab38361f1c0420709de97015cc68350c166d7f2d097ec1eecb0e2fad96efe2a7
data/README.md CHANGED
@@ -44,24 +44,37 @@ Before you can raise an error you'll need to register it first.
44
44
  Errawr.register!(:your_error)
45
45
  ```
46
46
 
47
- It's also possible to pass in additional metadata when registering an error.
47
+ ### Raising Errors
48
48
 
49
49
  ```ruby
50
- Errawr.register!(:bad_request, { http_status: 400 })
51
50
  begin
52
- Errawr.error!(:bad_request)
51
+ Errawr.error!(:your_error)
53
52
  rescue => e
54
- puts e.context[:http_status]
53
+ puts e.message # Localized error message defined in locale file
55
54
  end
56
55
  ```
57
56
 
58
- ### Raising Errors
57
+ ### Metadata
58
+
59
+ It's possible to add additional information to a registered error through metadata. Just specify a ```metadata``` hash when either registering:
59
60
 
60
61
  ```ruby
62
+ Errawr.register!(:your_error, metadata: { http_status: 400 })
61
63
  begin
62
64
  Errawr.error!(:your_error)
63
65
  rescue => e
64
- puts e.message # Localized error message defined in locale file
66
+ puts e.metadata[:http_status] # Will return 400
67
+ end
68
+ ```
69
+
70
+ or throwing an error:
71
+
72
+ ```ruby
73
+ Errawr.register!(:your_error)
74
+ begin
75
+ Errawr.error!(:your_error, metadata: { http_status: 400 })
76
+ rescue => e
77
+ puts e.metadata[:http_status] # Will return 400
65
78
  end
66
79
  ```
67
80
 
@@ -73,9 +86,9 @@ It's also possible to manage your errors and their metadata purely through local
73
86
  en:
74
87
  errawr:
75
88
  your_error:
76
- name: my error
77
- error:
78
- message: My awesome error message
89
+ message: My awesome error message
90
+ metadata:
91
+ http_status: 400
79
92
  ```
80
93
 
81
94
  Then just register and raise your exceptions like normal.
@@ -85,11 +98,29 @@ Errawr.register!(:your_error)
85
98
  begin
86
99
  Errawr.error!(:your_error)
87
100
  rescue => e
88
- puts e.context[:name] # Will return "my error"
101
+ puts e.metadata[:http_status] # Will return 400
89
102
  end
90
103
  ```
91
104
 
92
- **Note** the ```errawr.your_error.error.message``` locale key. This is the key used to define an error message when managing an error through a locale file.
105
+ ### I18n Interpolation
106
+
107
+ You can pass in parameter values to your localized error messages.
108
+
109
+ ```yaml
110
+ en
111
+ errawr:
112
+ your_error:
113
+ message: "My awesome error message is: %{error_message}"
114
+ ```
115
+
116
+ ```ruby
117
+ Errawr.register!(:your_error, error_message: 'You did it wrong!')
118
+ begin
119
+ Errawr.error!(:your_error)
120
+ rescue => e
121
+ puts e.message # Will return "My awesome error message is: You did it wrong!"
122
+ end
123
+ ```
93
124
 
94
125
  ### Overrides
95
126
 
@@ -99,25 +130,32 @@ Want to override that metadata you registered? That's cool too.
99
130
  en:
100
131
  errawr:
101
132
  your_error:
102
- name: my error
103
- error:
104
- message: My awesome error message
133
+ message: My awesome error message
134
+ metadata:
135
+ http_status: 400
105
136
  ```
106
137
 
138
+ The ```#register!``` method will override the locale file.
139
+
107
140
  ```ruby
108
- # #register! overrides metadata defined in the locale file
109
- Errawr.register!(:your_error, name: 'my infamous error')
141
+ Errawr.register!(:your_error, message: 'Some other error message', metadata: { http_status: 403 })
110
142
  begin
111
143
  Errawr.error!(:your_error)
112
144
  rescue => e
113
- puts e.context[:name] # Will return "my infamous error"
145
+ puts e.message # => Will return "Some other error message"
146
+ puts e.metadata[:http_status] # => Will return 403
114
147
  end
148
+ ```
149
+
150
+ The ```#error!``` method will override both ```#register!``` and the locale file.
115
151
 
152
+ ```ruby
153
+ Errawr.register!(:your_error, message: 'Some other error message', metadata: { http_status: 403 })
116
154
  begin
117
- # #error! metadata overrides both #register! and the locale file
118
- Errawr.error!(:your_error, name: 'my very favorite error')
155
+ Errawr.error!(:your_error, message: 'Yet another error message', metadata: { http_status: 404 })
119
156
  rescue => e
120
- puts e.context[:name] # Will return "my very favorite error"
157
+ puts e.message # => Will return "Yet another error message"
158
+ puts e.metadata[:http_status] # => Will return 404
121
159
  end
122
160
  ```
123
161
 
@@ -133,7 +171,7 @@ class YourError < Errawr::Error
133
171
  end
134
172
 
135
173
  # Register your error with your custom class
136
- Errawr.register!(:your_error, { error: { base_class: YourError } })
174
+ Errawr.register!(:your_error, base_class: YourError)
137
175
 
138
176
  # Do something with it
139
177
  begin
data/lib/errawr/error.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  module Errawr
2
2
  class Error < StandardError
3
- attr_reader :key, :context, :message
3
+ attr_reader :key, :context, :metadata, :message
4
4
 
5
5
  def initialize(key = :unknown, context = {})
6
6
  @key = key
7
7
  @i18n = I18n.t('errawr.' + @key.to_s)
8
8
  process_context(context)
9
+ process_metadata(context)
9
10
  process_message
10
11
  end
11
12
 
@@ -13,16 +14,23 @@ module Errawr
13
14
  def process_context(context)
14
15
  if @i18n.kind_of?(Hash)
15
16
  @context = @i18n.merge(context)
16
- @context.delete(:error)
17
+ @context.delete(:metadata)
17
18
  else
18
19
  @context = {}
19
20
  end
20
21
  end
21
22
 
23
+ def process_metadata(context)
24
+ if @i18n.kind_of?(Hash)
25
+ context = @i18n.merge(context)
26
+ end
27
+ @metadata = context.fetch(:metadata, {})
28
+ end
29
+
22
30
  def process_message
23
31
  if @i18n.kind_of?(Hash)
24
- key = 'errawr.' + @key.to_s + '.error.message'
25
- @message = I18n.t(key, @context, { default: I18n.t('errawr.unknown', @context) })
32
+ key = 'errawr.' + @key.to_s + '.message'
33
+ @message = I18n.t(key, @context.merge({ default: I18n.t('errawr.unknown', @context) }))
26
34
  else
27
35
  @message = I18n.t('errawr.' + @key.to_s, @context)
28
36
  end
data/lib/errawr/mapper.rb CHANGED
@@ -15,9 +15,9 @@ module Errawr
15
15
  end
16
16
 
17
17
  def self.register!(key, options = {})
18
- base_klass = options[:error] ? options[:error][:base_class] || Error : Error
18
+ base_klass = options[:base_class] || Error
19
19
  raise(ArgumentError, ':base_class must be a subclass of Errawr::Error') unless base_klass <= Error
20
- klass = Class.new(base_klass).new(key, options.select { |k, v| ![:error].include?(k) })
20
+ klass = Class.new(base_klass).new(key, options)
21
21
  add(klass)
22
22
  end
23
23
 
@@ -1,3 +1,3 @@
1
1
  module Errawr
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
data/lib/errawr.rb CHANGED
@@ -9,6 +9,7 @@ module Errawr
9
9
 
10
10
  def self.error!(name, context = {})
11
11
  klass = Mapper[name] || Mapper[:unknown]
12
+ klass.metadata.merge!(context.delete(:metadata) || {})
12
13
  klass.context.merge!(context)
13
14
  raise klass
14
15
  end
data/spec/errawr_spec.rb CHANGED
@@ -1,8 +1,64 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Errawr do
4
- it 'should raise an Errawr::Error exception' do
5
- Errawr.register!(:dummy_error)
6
- expect { Errawr.error!(:dummy_error) }.to raise_error(Errawr::Error)
4
+ describe 'error!' do
5
+ it 'should raise an Errawr::Error exception' do
6
+ Errawr.register!(:dummy_error)
7
+ expect { Errawr.error!(:dummy_error) }.to raise_error(Errawr::Error)
8
+ end
9
+
10
+ it 'should return custom metadata values using #register!' do
11
+ Errawr.register!(:some_error, metadata: { name: 'custom_register!_name' })
12
+ begin
13
+ Errawr.error!(:some_error)
14
+ rescue => e
15
+ e.metadata[:name].should == 'custom_register!_name'
16
+ end
17
+ end
18
+
19
+ it 'should return custom metadata values using #error!' do
20
+ Errawr.register!(:some_error, metadata: { name: 'custom_register!_name' })
21
+ begin
22
+ Errawr.error!(:some_error, metadata: { name: 'custom_error!_name' })
23
+ rescue => e
24
+ e.metadata[:name].should == 'custom_error!_name'
25
+ end
26
+ end
27
+
28
+ it 'should return custom metadata values from locale file' do
29
+ Errawr.register!(:error_hash)
30
+ begin
31
+ Errawr.error!(:error_hash)
32
+ rescue => e
33
+ e.metadata.include?(:name).should be_true
34
+ end
35
+ end
36
+
37
+ it '#register! should override custom metadata values from locale file' do
38
+ Errawr.register!(:error_hash, metadata: { name: 'register!_name' })
39
+ begin
40
+ Errawr.error!(:error_hash)
41
+ rescue => e
42
+ e.metadata[:name].should == 'register!_name'
43
+ end
44
+ end
45
+
46
+ it 'should override custom metadata values from #register! and locale file' do
47
+ Errawr.register!(:error_hash, metadata: { name: 'register!_name' })
48
+ begin
49
+ Errawr.error!(:error_hash, metadata: { name: 'error!_name' })
50
+ rescue => e
51
+ e.metadata[:name].should == 'error!_name'
52
+ end
53
+ end
54
+
55
+ it 'should interpolate locales' do
56
+ Errawr.register!(:interpolated_error, error_message: 'interpolated message')
57
+ begin
58
+ Errawr.error!(:interpolated_error)
59
+ rescue => e
60
+ e.message.should == 'Some error has occurred: interpolated message'
61
+ end
62
+ end
7
63
  end
8
64
  end
data/spec/error_spec.rb CHANGED
@@ -1,11 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Errawr::Error do
4
- before(:each) do
5
- I18n.load_path += Dir.glob('spec/support/en.yml')
6
- I18n.reload!
7
- end
8
-
9
4
  it 'should return a key of :unknown if no key is set' do
10
5
  error = Errawr::Error.new
11
6
  error.key.should == :unknown
@@ -28,6 +23,6 @@ describe Errawr::Error do
28
23
 
29
24
  it 'should insert custom values into the context if I18n value is a hash' do
30
25
  error = Errawr::Error.new(:error_hash)
31
- error.context[:name].should == 'error_name'
26
+ error.metadata[:name].should == 'error_name'
32
27
  end
33
28
  end
data/spec/mapper_spec.rb CHANGED
@@ -20,7 +20,7 @@ describe Errawr::Mapper do
20
20
 
21
21
  describe 'register!' do
22
22
  it 'should throw an error if base class is not a subclass of Errawr::Error' do
23
- expect { Errawr.register!(:dummy_error, { error: { base_class: StandardError } }) }.to raise_error(ArgumentError)
23
+ expect { Errawr.register!(:dummy_error, base_class: StandardError) }.to raise_error(ArgumentError)
24
24
  end
25
25
 
26
26
  it 'should add an error to stored errors' do
@@ -28,41 +28,12 @@ describe Errawr::Mapper do
28
28
  Errawr::Mapper[:dummy_error].should_not be_nil
29
29
  end
30
30
 
31
- it 'should not add :error options to context' do
32
- Errawr.register!(:dummy_error, error: { name: :same_dummy_error })
31
+ it 'should not add :metadata options to context' do
32
+ Errawr.register!(:dummy_error, metadata: { name: :same_dummy_error })
33
33
  begin
34
34
  Errawr.error!(:dummy_error)
35
35
  rescue => e
36
- e.context.include?(:error).should be_false
37
- end
38
- end
39
- end
40
-
41
- describe 'error!' do
42
- it 'should return custom context values from locale file' do
43
- Errawr.register!(:error_hash)
44
- begin
45
- Errawr.error!(:error_hash)
46
- rescue => e
47
- e.context.include?(:name).should be_true
48
- end
49
- end
50
-
51
- it '#register! should override custom context values from locale file' do
52
- Errawr.register!(:error_hash, name: 'register!_name')
53
- begin
54
- Errawr.error!(:error_hash)
55
- rescue => e
56
- e.context[:name].should == 'register!_name'
57
- end
58
- end
59
-
60
- it '#error! should override custom context values from #register! and locale file' do
61
- Errawr.register!(:error_hash, name: 'register!_name')
62
- begin
63
- Errawr.error!(:error_hash, name: 'error!_name')
64
- rescue => e
65
- e.context[:name].should == 'error!_name'
36
+ e.metadata.include?(:error).should be_false
66
37
  end
67
38
  end
68
39
  end
data/spec/spec_helper.rb CHANGED
@@ -5,4 +5,11 @@ require 'errawr'
5
5
 
6
6
  # Require supporting ruby files with custom matchers and macros, etc,
7
7
  # in spec/support/ and its subdirectories.
8
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| puts f; require f }
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| puts f; require f }
9
+
10
+ RSpec.configure do |config|
11
+ config.before(:each) do
12
+ I18n.load_path += Dir.glob('spec/support/en.yml')
13
+ I18n.reload!
14
+ end
15
+ end
data/spec/support/en.yml CHANGED
@@ -2,8 +2,11 @@ en:
2
2
  errawr:
3
3
  some_error: Some error has occurred
4
4
  error_hash:
5
- name: error_name
6
- error:
7
- message: Some hashed error has occurred
5
+ message: Some hashed error has occurred
6
+ metadata:
7
+ name: error_name
8
8
  bad_error_hash:
9
- name: bad_error
9
+ metadata:
10
+ name: bad_error
11
+ interpolated_error:
12
+ message: "Some error has occurred: %{error_message}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: errawr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-16 00:00:00.000000000 Z
11
+ date: 2013-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n