errawr 1.1.6 → 1.2.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: be2440444126c0bf8520e79f82f1390e69840420
4
- data.tar.gz: 54c653df41721042a39e81858e3f2fc9d5bc164b
3
+ metadata.gz: b15331e311d4b2350523dcf6d915e6b911c5aa1d
4
+ data.tar.gz: c3652e013846e44f69b465f774bbe95282e8312d
5
5
  SHA512:
6
- metadata.gz: eebb52e2d62840f94662258155c5d65b3b1d247b7d35ff89a6c0e8f1f6feb59332f7b1d55d7f797b4a23668c1009c000b9be67c9cf9669168b78795ff197b3ee
7
- data.tar.gz: 789f8f217c980bd913d90bb0825631e8e79dba6c4f195a94b0acb7aea0bfd38f18f27a6d3dc767199d800ef3a037af1a240aac3e3253b69ac9ec85f64e2d12c7
6
+ metadata.gz: 097272fcd210ba08660135400117c20ca658cb208d75989fdd67be7800020bca07c7fc687c78e04a49725378517838351a64e3dc65424b4e34f5f259a6b842b2
7
+ data.tar.gz: e9a7cbbb11b7ab43296b396fe741368a88cd51ec7f7f367f3401cb480f98160b19a4916e8ddd9633a85f826c4e347acf3e521dfd38fa2021c88f1781f75398e2
@@ -2,7 +2,13 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.0
6
+ - 2.2.0
5
7
  - ruby-head
6
8
  - jruby-19mode
7
9
  - jruby-head
8
- - rbx
10
+ - rbx
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ - rvm: jruby-head
data/Gemfile CHANGED
@@ -6,17 +6,9 @@ gemspec
6
6
  group :development do
7
7
  gem 'bundler'
8
8
  gem 'coveralls', require: false
9
- gem 'rake'
10
9
  end
11
10
 
12
11
  group :test do
13
12
  gem 'rspec'
14
13
  gem 'simplecov'
15
14
  end
16
-
17
- platforms :rbx do
18
- gem 'racc'
19
- gem 'rubinius-coverage', github: 'rubinius/rubinius-coverage'
20
- gem 'rubysl'
21
- gem 'rubysl-json'
22
- end
data/README.md CHANGED
@@ -20,46 +20,24 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- ### Localizations
23
+ ### Defining and Raising Errors
24
24
 
25
- Errawr uses [I18n](https://github.com/svenfuchs/i18n) for easily managing error localizations. Just define a locale using the ```errawr``` key.
25
+ Errawr uses [I18n](https://github.com/svenfuchs/i18n) for easily managing error
26
+ localizations. Just define an error in a locale file. Make sure to use the
27
+ ```errawr``` key.
26
28
 
27
29
  ```yaml
28
30
  en:
29
31
  errawr:
30
- your_error: Your error message here
31
- ```
32
-
33
- Need to add more locale files? Use I18n's standard ```load_path```.
34
-
35
- ```ruby
36
- I18n.load_path += Dir.glob('lib/your_lib/locales/*.{rb,yml}')
37
- ```
38
-
39
- ### Registering Errors
40
-
41
- Before you can raise an error you'll need to register it first.
42
-
43
- ```ruby
44
- Errawr.register!(:your_error)
32
+ your_error:
33
+ message: My awesome error message
34
+ metadata:
35
+ http_status: 400
45
36
  ```
46
37
 
47
- ### Raising Errors
38
+ Then just raise your exception using the `#error!` method.
48
39
 
49
40
  ```ruby
50
- begin
51
- Errawr.error!(:your_error)
52
- rescue => e
53
- puts e.message # Localized error message defined in locale file
54
- end
55
- ```
56
-
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:
60
-
61
- ```ruby
62
- Errawr.register!(:your_error, metadata: { http_status: 400 })
63
41
  begin
64
42
  Errawr.error!(:your_error)
65
43
  rescue => e
@@ -67,36 +45,20 @@ rescue => e
67
45
  end
68
46
  ```
69
47
 
70
- or throwing an error:
48
+ Need to add more locale files? Use I18n's standard ```load_path```.
71
49
 
72
50
  ```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
78
- end
51
+ I18n.load_path += Dir.glob('lib/your_lib/locales/*.{rb,yml}')
79
52
  ```
80
53
 
81
- ### Managing Errors through Locale Files
82
-
83
- It's also possible to manage your errors and their metadata purely through locale files.
84
-
85
- ```yaml
86
- en:
87
- errawr:
88
- your_error:
89
- message: My awesome error message
90
- metadata:
91
- http_status: 400
92
- ```
54
+ ### Metadata
93
55
 
94
- Then just register and raise your exceptions like normal.
56
+ It's possible to add additional information to a registered error through
57
+ metadata. Just specify a ```metadata``` hash when throwing an error:
95
58
 
96
59
  ```ruby
97
- Errawr.register!(:your_error)
98
60
  begin
99
- Errawr.error!(:your_error)
61
+ Errawr.error!(:your_error, metadata: { http_status: 400 })
100
62
  rescue => e
101
63
  puts e.metadata[:http_status] # Will return 400
102
64
  end
@@ -114,9 +76,8 @@ en
114
76
  ```
115
77
 
116
78
  ```ruby
117
- Errawr.register!(:your_error, error_message: 'You did it wrong!')
118
79
  begin
119
- Errawr.error!(:your_error)
80
+ Errawr.error!(:your_error, error_message: 'You did it wrong!')
120
81
  rescue => e
121
82
  puts e.message # Will return "My awesome error message is: You did it wrong!"
122
83
  end
@@ -124,7 +85,8 @@ end
124
85
 
125
86
  ### Overrides
126
87
 
127
- Want to override that metadata you registered? That's cool too.
88
+ It's possible to override metadata stored in a locale file both globally and
89
+ on a per use basis.
128
90
 
129
91
  ```yaml
130
92
  en:
@@ -157,7 +119,7 @@ rescue => e
157
119
  puts e.message # => Will return "Yet another error message"
158
120
  puts e.metadata[:http_status] # => Will return 404
159
121
  end
160
- ```
122
+ ```
161
123
 
162
124
  ### Custom Error Classes
163
125
 
@@ -194,4 +156,4 @@ end
194
156
 
195
157
  Errawr is maintained and funded by [Sticksnleaves](http://www.sticksnleaves.com)
196
158
 
197
- Thanks to all of our [contributors](https://github.com/anthonator/errawr/graphs/contributors)
159
+ Thanks to all of our [contributors](https://github.com/anthonator/errawr/graphs/contributors)
@@ -7,24 +7,24 @@ require 'errawr/version'
7
7
  module Errawr
8
8
  I18n.load_path << "#{File.dirname(__FILE__)}/errawr/locales/en.yml"
9
9
  I18n.reload!
10
-
10
+
11
11
  def self.included(base)
12
12
  base.extend(ClassMethods)
13
13
  end
14
-
14
+
15
15
  module ClassMethods
16
16
  def error!(name, context = {})
17
- klass = Mapper[name] || Mapper[:unknown]
17
+ klass = Mapper[name] || Error.new(name, context)
18
18
  klass.update_context(context) unless context.empty?
19
- raise klass
19
+ fail klass
20
20
  end
21
21
 
22
22
  def register!(key, options = {})
23
23
  Mapper.register!(key, options)
24
24
  end
25
25
  end
26
-
26
+
27
27
  extend ClassMethods
28
-
28
+
29
29
  register!(:unknown)
30
30
  end
@@ -1,37 +1,38 @@
1
1
  module Errawr
2
2
  class Error < StandardError
3
3
  attr_reader :key, :context, :metadata
4
-
4
+
5
5
  def initialize(key = :unknown, context = {})
6
6
  @key = key
7
- @context = context
7
+ @context = context.dup
8
8
  @metadata = {}
9
9
  @i18n = I18n.t('errawr.' + @key.to_s)
10
10
  update_context(@context)
11
11
  end
12
-
12
+
13
13
  def message
14
14
  process_message
15
15
  end
16
-
16
+
17
17
  def update_context(context)
18
18
  process_context(context)
19
19
  end
20
-
20
+
21
21
  private
22
+
22
23
  def process_context(context)
23
24
  @message_overridden = context.include?(:message)
24
- if @i18n.kind_of?(Hash)
25
+ if @i18n.is_a?(Hash)
25
26
  @context.merge!(@i18n.merge(context))
26
27
  else
27
28
  @context.merge!(context)
28
29
  end
29
- @metadata.merge!(@context.delete(:metadata)) if @context.has_key?(:metadata)
30
+ @metadata.merge!(@context.delete(:metadata)) if @context.key?(:metadata)
30
31
  end
31
-
32
+
32
33
  def process_message
33
34
  return @context[:message] if @message_overridden
34
- if @i18n.kind_of?(Hash)
35
+ if @i18n.is_a?(Hash)
35
36
  key = 'errawr.' + @key.to_s + '.message'
36
37
  @context[:message] = I18n.t(key, @context.merge({ default: I18n.t('errawr.unknown', @context) }))
37
38
  else
@@ -40,4 +41,4 @@ module Errawr
40
41
  @context[:message]
41
42
  end
42
43
  end
43
- end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module Errawr
2
- VERSION = '1.1.6'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -3,69 +3,66 @@ require 'spec_helper'
3
3
  describe Errawr do
4
4
  describe 'error!' do
5
5
  it 'should raise an Errawr::Error exception' do
6
- Errawr.register!(:dummy_error)
7
6
  expect { Errawr.error!(:dummy_error) }.to raise_error(Errawr::Error)
8
7
  end
9
-
8
+
10
9
  it 'should return custom metadata values using #register!' do
11
10
  Errawr.register!(:some_error, metadata: { name: 'custom_register!_name' })
12
11
  begin
13
12
  Errawr.error!(:some_error)
14
13
  rescue => e
15
- e.metadata[:name].should == 'custom_register!_name'
14
+ expect(e.metadata[:name]).to eq('custom_register!_name')
16
15
  end
17
16
  end
18
-
19
- it 'should return custom metadata values using #error!' do
17
+
18
+ it 'should return custom metadata set with #register! using #error!' do
20
19
  Errawr.register!(:some_error, metadata: { name: 'custom_register!_name' })
21
20
  begin
22
21
  Errawr.error!(:some_error, metadata: { name: 'custom_error!_name' })
23
22
  rescue => e
24
- e.metadata[:name].should == 'custom_error!_name'
23
+ expect(e.metadata[:name]).to eq('custom_error!_name')
25
24
  end
26
25
  end
27
-
26
+
28
27
  it 'should return custom metadata values from locale file' do
29
- Errawr.register!(:error_hash)
30
28
  begin
31
29
  Errawr.error!(:error_hash)
32
30
  rescue => e
33
- e.metadata.include?(:name).should be_true
31
+ expect(e.metadata).to include(:name)
34
32
  end
35
33
  end
36
-
37
- it 'should override custom metadata values from #register! and locale file' do
34
+
35
+ it 'should override custom metadata values from #register! and locale file using #error!' do
38
36
  Errawr.register!(:error_hash, metadata: { name: 'register!_name' })
39
37
  begin
40
38
  Errawr.error!(:error_hash, metadata: { name: 'error!_name' })
41
39
  rescue => e
42
- e.metadata[:name].should == 'error!_name'
40
+ expect(e.metadata[:name]).to eq('error!_name')
43
41
  end
44
42
  end
45
-
43
+
46
44
  it 'should return an overridden message for a non-hashed locale' do
47
45
  begin
48
46
  Errawr.error!(:some_error, message: 'Overridden error message')
49
47
  rescue => e
50
- e.message.should == 'Overridden error message'
48
+ expect(e.message).to eq('Overridden error message')
51
49
  end
52
50
  end
53
-
51
+
54
52
  it 'should return an overridden message for a hashed locale' do
55
53
  begin
56
54
  Errawr.error!(:error_hash, message: 'Overridden error message')
57
55
  rescue => e
58
- e.message.should == 'Overridden error message'
56
+ expect(e.message).to eq('Overridden error message')
59
57
  end
60
58
  end
61
-
59
+
62
60
  it 'should pass in interpolated params' do
63
- Errawr.register!(:interpolated_error)
64
61
  begin
65
62
  Errawr.error!(:interpolated_error, error_message: 'interpolated message')
66
63
  rescue => e
67
- e.message.should == 'Some error has occurred: interpolated message'
64
+ expect(e.message).to eq('Some error has occurred: interpolated message')
68
65
  end
69
66
  end
70
67
  end
71
- end
68
+ end
@@ -3,21 +3,21 @@ require 'spec_helper'
3
3
  describe Errawr::Error do
4
4
  it 'should return a key of :unknown if no key is set' do
5
5
  error = Errawr::Error.new
6
- error.key.should == :unknown
6
+ expect(error.key).to eq(:unknown)
7
7
  end
8
-
8
+
9
9
  it 'should return a localized message' do
10
10
  error = Errawr::Error.new(:some_error)
11
- error.message.should == 'Some error has occurred'
11
+ expect(error.message).to eq('Some error has occurred')
12
12
  end
13
-
13
+
14
14
  it 'should return a localized message if I18n value is a hash' do
15
15
  error = Errawr::Error.new(:error_hash)
16
- error.message.should == 'Some hashed error has occurred'
16
+ expect(error.message).to eq('Some hashed error has occurred')
17
17
  end
18
-
18
+
19
19
  it 'should return an unknown error if error => message is not provided' do
20
20
  error = Errawr::Error.new(:bad_error_hash)
21
- error.message.should == 'An unknown error has occurred'
21
+ expect(error.message).to eq('An unknown error has occurred')
22
22
  end
23
- end
23
+ end
@@ -2,75 +2,70 @@ require 'spec_helper'
2
2
 
3
3
  describe Errawr::Mapper do
4
4
  it 'should automatically register an unknown error' do
5
- Errawr::Mapper.all[:unknown].should_not be_nil
5
+ expect(Errawr::Mapper.all[:unknown]).to_not eq(nil)
6
6
  end
7
-
7
+
8
8
  describe '[]' do
9
9
  it 'should return stored errors' do
10
- Errawr::Mapper[:unknown].should_not be_nil
10
+ expect(Errawr::Mapper[:unknown]).to_not eq(nil)
11
11
  end
12
12
  end
13
-
13
+
14
14
  describe 'add' do
15
15
  it 'should add an error to stored errors' do
16
16
  Errawr::Mapper.add(DummyError.new)
17
- Errawr::Mapper[:dummy_key].should_not be_nil
17
+ expect(Errawr::Mapper[:dummy_key]).to_not eq(nil)
18
18
  end
19
19
  end
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
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
27
27
  Errawr.register!(:dummy_error)
28
- Errawr::Mapper[:dummy_error].should_not be_nil
28
+ expect(Errawr::Mapper[:dummy_error]).to_not be_nil
29
29
  end
30
-
30
+
31
31
  it 'should not add :metadata options to context' do
32
- Errawr.register!(:dummy_error, metadata: { name: :same_dummy_error })
33
32
  begin
34
- Errawr.error!(:dummy_error)
33
+ Errawr.error!(:dummy_error, metadata: { name: :same_dummy_error })
35
34
  rescue => e
36
- e.metadata.include?(:error).should be_false
35
+ expect(e.metadata).to_not include(:error)
37
36
  end
38
37
  end
39
-
38
+
40
39
  it 'should return an overridden message for a non-hashed locale' do
41
- Errawr.register!(:some_error, message: 'Overridden error message')
42
40
  begin
43
- Errawr.error!(:some_error)
41
+ Errawr.error!(:some_error, message: 'Overridden error message')
44
42
  rescue => e
45
- e.message.should == 'Overridden error message'
43
+ expect(e.message).to eq('Overridden error message')
46
44
  end
47
45
  end
48
-
46
+
49
47
  it 'should return an overridden message for a hashed locale' do
50
- Errawr.register!(:error_hash, message: 'Overridden error message')
51
48
  begin
52
- Errawr.error!(:error_hash)
49
+ Errawr.error!(:error_hash, message: 'Overridden error message')
53
50
  rescue => e
54
- e.message.should == 'Overridden error message'
51
+ expect(e.message).to eq('Overridden error message')
55
52
  end
56
53
  end
57
-
54
+
58
55
  it 'should override custom metadata values from locale file' do
59
- Errawr.register!(:error_hash, metadata: { name: 'register!_name' })
60
56
  begin
61
- Errawr.error!(:error_hash)
57
+ Errawr.error!(:error_hash, metadata: { name: 'register!_name' })
62
58
  rescue => e
63
- e.metadata[:name].should == 'register!_name'
59
+ expect(e.metadata[:name]).to eq('register!_name')
64
60
  end
65
61
  end
66
-
62
+
67
63
  it 'should interpolate locales' do
68
- Errawr.register!(:interpolated_error, error_message: 'interpolated message')
69
64
  begin
70
- Errawr.error!(:interpolated_error)
65
+ Errawr.error!(:interpolated_error, error_message: 'interpolated message')
71
66
  rescue => e
72
- e.message.should == 'Some error has occurred: interpolated message'
67
+ expect(e.message).to eq('Some error has occurred: interpolated message')
73
68
  end
74
69
  end
75
70
  end
76
- end
71
+ end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: errawr
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.6
4
+ version: 1.2.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-22 00:00:00.000000000 Z
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Easily define and raise localized errors.
@@ -59,8 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
63
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
64
  - Gemfile
65
65
  - LICENSE.txt
66
66
  - README.md
@@ -87,17 +87,17 @@ require_paths:
87
87
  - lib
88
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - '>='
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.0.3
100
+ rubygems_version: 2.4.5
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: A framework for effectively defining and raising localized errors.