human_error 3.0.1 → 3.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: ae7626c4412e2cb61249a059dffe4ce61a0a5781
4
- data.tar.gz: 2d86051488201bfa3edb0ec68994d6025fd1316c
3
+ metadata.gz: 8bcd73a9e09835dff5279ebae166f1b379ff3b26
4
+ data.tar.gz: ce33687c79d3c4a7b00e14603bbccd77dad48feb
5
5
  SHA512:
6
- metadata.gz: 7b5a792a22cb283d12e9170cb50c5f91c9efbcde54c1b94d8950b37e2860a585716a669ecc54a92634869b4996801a081b72ec86f656cdb5d6f6ee8635f4b6f5
7
- data.tar.gz: 0883edbf5941bfc16d7aa86f41d192130c2ddc0c82e6da88515214b36789aab72e77a4720296c67dbd657f66c7ce716a9d7be9eca2cc353c86b193d8e9b15cf5
6
+ metadata.gz: 4c2de3573c9de4666ea9810dc849a76924fc7ec592f2615fbc9904bf7a7f0ed3dfc5f035a363f32c92a1751e83aa7d1189432cb45ecb84cb4864d648690630c9
7
+ data.tar.gz: f373af39d2dfe44009ed2a3901cf66aaefe0ea48898f6bc74c032dc3f04294bd123b3ac3073d261007ad01f6852a6a53a84319a9118fed3d656ec5d50ccb9f8e
data/lib/human_error.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'human_error/configuration'
2
2
  require 'human_error/error'
3
+ require 'human_error/errors/authorization_error'
4
+ require 'human_error/errors/authorization_errors/forbidden_error'
3
5
  require 'human_error/errors/authentication_error'
4
6
  require 'human_error/errors/authentication_errors/duplicate_authentication_error'
5
7
  require 'human_error/errors/authentication_errors/invalid_token_error'
@@ -40,6 +42,6 @@ class HumanError
40
42
  end
41
43
 
42
44
  def self.raise(error_type, **args)
43
- Kernel.raise build(error_type, **args)
45
+ Kernel.fail build(error_type, **args)
44
46
  end
45
47
  end
@@ -1,3 +1,5 @@
1
+ require 'human_error/error'
2
+
1
3
  class HumanError
2
4
  module Errors
3
5
  module AuthenticationError
@@ -0,0 +1,8 @@
1
+ require 'human_error/error'
2
+
3
+ class HumanError
4
+ module Errors
5
+ module AuthorizationError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,47 @@
1
+ require 'human_error/errors/authorization_error'
2
+
3
+ class HumanError
4
+ module Errors
5
+ class ForbiddenError < RuntimeError
6
+ include Error
7
+ include AuthorizationError
8
+
9
+ NON_SPECIFIC_RESOURCE_ACTIONS = %w{index create}.freeze
10
+
11
+ attr_accessor :resource_name,
12
+ :resource_id,
13
+ :action
14
+
15
+ def http_status
16
+ 403
17
+ end
18
+
19
+ def title
20
+ 'Forbidden'
21
+ end
22
+
23
+ def detail
24
+ detail_quantity = if NON_SPECIFIC_RESOURCE_ACTIONS.include? action.to_s
25
+ "#{action} a #{resource_name}"
26
+ else
27
+ "#{action} the #{resource_name} with ID #{resource_id}"
28
+ end
29
+
30
+ "You do not have access to #{detail_quantity}. Providing a different set of " \
31
+ 'credentials may potentially allow you access to this resource.'
32
+ end
33
+
34
+ def source
35
+ @source ||= {
36
+ 'resource_name' => resource_name,
37
+ 'resource_id' => resource_id,
38
+ 'action' => @action,
39
+ }
40
+ end
41
+
42
+ def action
43
+ @action || 'access'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,3 @@
1
1
  class HumanError
2
- VERSION = '3.0.1'
2
+ VERSION = '3.1.0'.freeze
3
3
  end
@@ -0,0 +1,51 @@
1
+ require 'rspectacular'
2
+ require 'human_error/errors/authorization_errors/forbidden_error'
3
+
4
+ class HumanError
5
+ module Errors
6
+ describe ForbiddenError do
7
+ let(:error) { ForbiddenError.new }
8
+
9
+ it 'has a status of 403' do
10
+ expect(error.http_status).to eql 403
11
+ end
12
+
13
+ it 'has a code' do
14
+ expect(error.code).to eql 'errors.forbidden_error'
15
+ end
16
+
17
+ it 'has a title' do
18
+ expect(error.title).to eql 'Forbidden'
19
+ end
20
+
21
+ it 'can output the detail for actions affecting a specific resource' do
22
+ error = ForbiddenError.new(resource_name: 'resource',
23
+ resource_id: '123',
24
+ action: 'show')
25
+
26
+ expect(error.detail).to eql \
27
+ 'You do not have access to show the resource with ID 123. Providing a different ' \
28
+ 'set of credentials may potentially allow you access to this resource.'
29
+ end
30
+
31
+ it 'can output the detail for actions affecting multiple resources' do
32
+ error = ForbiddenError.new(resource_name: 'resource',
33
+ action: 'create')
34
+
35
+ expect(error.detail).to eql \
36
+ 'You do not have access to create a resource. Providing a different ' \
37
+ 'set of credentials may potentially allow you access to this resource.'
38
+ end
39
+
40
+ it 'can output the source' do
41
+ error = ForbiddenError.new(resource_name: 'resource',
42
+ resource_id: '123',
43
+ action: 'show')
44
+
45
+ expect(error.source).to eql('resource_name' => 'resource',
46
+ 'resource_id' => '123',
47
+ 'action' => 'show')
48
+ end
49
+ end
50
+ end
51
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jfelchner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-12 00:00:00.000000000 Z
11
+ date: 2015-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -82,6 +82,8 @@ files:
82
82
  - lib/human_error/errors/authentication_errors/duplicate_authentication_error.rb
83
83
  - lib/human_error/errors/authentication_errors/invalid_token_error.rb
84
84
  - lib/human_error/errors/authentication_errors/invalid_username_or_password_error.rb
85
+ - lib/human_error/errors/authorization_error.rb
86
+ - lib/human_error/errors/authorization_errors/forbidden_error.rb
85
87
  - lib/human_error/errors/crud_error.rb
86
88
  - lib/human_error/errors/crud_errors/association_error.rb
87
89
  - lib/human_error/errors/crud_errors/resource_not_found_error.rb
@@ -97,6 +99,7 @@ files:
97
99
  - spec/lib/human_error/errors/authentication_errors/duplicate_authentication_error_spec.rb
98
100
  - spec/lib/human_error/errors/authentication_errors/invalid_token_error_spec.rb
99
101
  - spec/lib/human_error/errors/authentication_errors/invalid_username_or_password_error_spec.rb
102
+ - spec/lib/human_error/errors/authorization_errors/forbidden_error_spec.rb
100
103
  - spec/lib/human_error/errors/crud_errors/association_error_spec.rb
101
104
  - spec/lib/human_error/errors/crud_errors/resource_not_found_error_spec.rb
102
105
  - spec/lib/human_error/errors/crud_errors/resource_persistence_error_spec.rb
@@ -134,6 +137,7 @@ test_files:
134
137
  - spec/lib/human_error/errors/authentication_errors/duplicate_authentication_error_spec.rb
135
138
  - spec/lib/human_error/errors/authentication_errors/invalid_token_error_spec.rb
136
139
  - spec/lib/human_error/errors/authentication_errors/invalid_username_or_password_error_spec.rb
140
+ - spec/lib/human_error/errors/authorization_errors/forbidden_error_spec.rb
137
141
  - spec/lib/human_error/errors/crud_errors/association_error_spec.rb
138
142
  - spec/lib/human_error/errors/crud_errors/resource_not_found_error_spec.rb
139
143
  - spec/lib/human_error/errors/crud_errors/resource_persistence_error_spec.rb