human_error 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/human_error.rb +21 -19
  3. data/lib/human_error/configuration.rb +27 -7
  4. data/lib/human_error/error.rb +60 -28
  5. data/lib/human_error/errors/authentication_errors/duplicate_authentication_error.rb +8 -8
  6. data/lib/human_error/errors/authentication_errors/invalid_token_error.rb +8 -8
  7. data/lib/human_error/errors/authentication_errors/invalid_username_or_password_error.rb +8 -8
  8. data/lib/human_error/errors/crud_errors/association_error.rb +8 -9
  9. data/lib/human_error/errors/crud_errors/resource_not_found_error.rb +8 -8
  10. data/lib/human_error/errors/crud_errors/resource_persistence_error.rb +8 -8
  11. data/lib/human_error/errors/request_errors/parameter_missing_error.rb +40 -0
  12. data/lib/human_error/errors/request_errors/unpermitted_parameters_error.rb +46 -0
  13. data/lib/human_error/rescuable_resource.rb +30 -47
  14. data/lib/human_error/verifiable_resource.rb +36 -0
  15. data/lib/human_error/version.rb +1 -1
  16. data/spec/lib/human_error/configuration_spec.rb +13 -26
  17. data/spec/lib/human_error/error_spec.rb +135 -1
  18. data/spec/lib/human_error/errors/authentication_errors/duplicate_authentication_error_spec.rb +8 -13
  19. data/spec/lib/human_error/errors/authentication_errors/invalid_token_error_spec.rb +8 -12
  20. data/spec/lib/human_error/errors/authentication_errors/invalid_username_or_password_error_spec.rb +8 -13
  21. data/spec/lib/human_error/errors/crud_errors/association_error_spec.rb +8 -16
  22. data/spec/lib/human_error/errors/crud_errors/resource_not_found_error_spec.rb +9 -17
  23. data/spec/lib/human_error/errors/crud_errors/resource_persistence_error_spec.rb +9 -17
  24. data/spec/lib/human_error/errors/request_errors/parameter_missing_error_spec.rb +55 -0
  25. data/spec/lib/human_error/errors/request_errors/unpermitted_parameters_error_spec.rb +62 -0
  26. data/spec/lib/human_error_spec.rb +4 -57
  27. metadata +26 -12
  28. data/lib/human_error/error_code_directory.rb +0 -20
  29. data/lib/human_error/errors.rb +0 -9
  30. data/lib/human_error/errors/request_error.rb +0 -48
  31. data/lib/human_error/knowledgebase_id_directory.rb +0 -20
  32. data/lib/human_error/verifiable_model.rb +0 -30
  33. data/spec/lib/human_error/errors/request_error_spec.rb +0 -95
@@ -10,26 +10,22 @@ describe InvalidTokenError do
10
10
  expect(error.http_status).to eql 401
11
11
  end
12
12
 
13
- it 'has a code of 1003' do
14
- expect(error.code).to eql 1003
13
+ it 'has a code' do
14
+ expect(error.code).to eql 'errors.invalid_token_error'
15
15
  end
16
16
 
17
- it 'has a knowledgebase article ID of 1234567890' do
18
- expect(error.knowledgebase_article_id).to eql '1234567890'
17
+ it 'has a title' do
18
+ expect(error.title).to eql 'Invalid Token'
19
19
  end
20
20
 
21
- it 'can output the developer message' do
22
- expect(error.developer_message).to eql 'The token you attempted to use for this ' \
21
+ it 'can output the detail' do
22
+ expect(error.detail).to eql 'The token you attempted to use for this ' \
23
23
  'request is invalid for this resource. ' \
24
24
  'Please double-check and try again.'
25
25
  end
26
26
 
27
- it 'can output the developer details' do
28
- expect(error.developer_details).to eql(token: '[FILTERED]')
29
- end
30
-
31
- it 'can output the friendly message' do
32
- expect(error.friendly_message).to eql 'Sorry! You are not authorized to view this.'
27
+ it 'can output the source' do
28
+ expect(error.source).to eql(token: '[FILTERED]')
33
29
  end
34
30
  end
35
31
  end
@@ -10,29 +10,24 @@ describe InvalidUsernameOrPasswordError do
10
10
  expect(error.http_status).to eql 401
11
11
  end
12
12
 
13
- it 'has a code of 1004' do
14
- expect(error.code).to eql 1004
13
+ it 'has a code' do
14
+ expect(error.code).to eql 'errors.invalid_username_or_password_error'
15
15
  end
16
16
 
17
- it 'has a knowledgebase article ID of 1234567890' do
18
- expect(error.knowledgebase_article_id).to eql '1234567890'
17
+ it 'has a title' do
18
+ expect(error.title).to eql 'Invalid Username/Password'
19
19
  end
20
20
 
21
- it 'can output the developer message' do
22
- expect(error.developer_message).to eql 'Either the username or password passed in ' \
21
+ it 'can output the detail' do
22
+ expect(error.detail).to eql 'Either the username or password passed in ' \
23
23
  'or this request is invalid. Please ' \
24
24
  'double-check and try again.'
25
25
  end
26
26
 
27
- it 'can output the developer details' do
27
+ it 'can output the source' do
28
28
  error = InvalidUsernameOrPasswordError.new username: 'neo'
29
29
 
30
- expect(error.developer_details).to eql(username: 'neo', password: '[FILTERED]')
31
- end
32
-
33
- it 'can output the friendly message' do
34
- expect(error.friendly_message).to eql 'Either your email or password is ' \
35
- 'incorrect. Please double-check and try again.'
30
+ expect(error.source).to eql(username: 'neo', password: '[FILTERED]')
36
31
  end
37
32
  end
38
33
  end
@@ -15,47 +15,39 @@ describe AssociationError do
15
15
  expect(error.http_status).to eql 422
16
16
  end
17
17
 
18
- it 'has a code of 1009' do
18
+ it 'has a code' do
19
19
  error = AssociationError.new
20
20
 
21
- expect(error.code).to eql 1009
21
+ expect(error.code).to eql 'errors.association_error'
22
22
  end
23
23
 
24
- it 'has a knowledgebase article ID of 1234567890' do
24
+ it 'has a title' do
25
25
  error = AssociationError.new
26
26
 
27
- expect(error.knowledgebase_article_id).to eql '1234567890'
27
+ expect(error.title).to eql 'Association Error'
28
28
  end
29
29
 
30
- it 'includes the resource name and action in the developer message' do
30
+ it 'includes the resource name and action in the detail' do
31
31
  error = AssociationError.new association_name: 'black leather trenchcoat',
32
32
  resource_name: 'Neo'
33
33
 
34
- expect(error.developer_message).to eql 'The black leather trenchcoat that you ' \
34
+ expect(error.detail).to eql 'The black leather trenchcoat that you ' \
35
35
  'attempted to associate with the Neo was ' \
36
36
  'not valid.'
37
37
  end
38
38
 
39
- it 'includes the resource name and action in the developer details' do
39
+ it 'includes the resource name and action in the source' do
40
40
  error = AssociationError.new association_name: 'black leather trenchcoat',
41
41
  resource_name: 'Neo',
42
42
  attributes: 'what is the matrix',
43
43
  association_id: '123'
44
44
 
45
- expect(error.developer_details).to eql(
45
+ expect(error.source).to eql(
46
46
  'Neo' => 'what is the matrix',
47
47
  'black leather trenchcoat id' => '123',
48
48
  )
49
49
  end
50
50
 
51
- it 'includes the resource name and action in the friendly message' do
52
- error = AssociationError.new association_name: 'black leather trenchcoat',
53
- resource_name: 'Neo'
54
-
55
- expect(error.friendly_message).to eql 'Sorry! There was a problem when we tried to ' \
56
- 'set the black leather trenchcoat on that Neo.'
57
- end
58
-
59
51
  it 'can convert an "ActiveRecord::InvalidForeignKey"' do
60
52
  error = AssociationError.convert(foreign_key_error)
61
53
 
@@ -11,48 +11,40 @@ describe ResourceNotFoundError do
11
11
  expect(error.http_status).to eql 404
12
12
  end
13
13
 
14
- it 'has a code of 1005' do
14
+ it 'has a code' do
15
15
  error = ResourceNotFoundError.new
16
16
 
17
- expect(error.code).to eql 1005
17
+ expect(error.code).to eql 'errors.resource_not_found_error'
18
18
  end
19
19
 
20
- it 'has a knowledgebase article ID of 1234567890' do
20
+ it 'has a title' do
21
21
  error = ResourceNotFoundError.new
22
22
 
23
- expect(error.knowledgebase_article_id).to eql '1234567890'
23
+ expect(error.title).to eql 'Resource Not Found'
24
24
  end
25
25
 
26
- it 'includes the resource name and action in the developer message' do
26
+ it 'includes the resource name and action in the detail' do
27
27
  error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
28
28
  action: 'bullet time'
29
29
 
30
- expect(error.developer_message).to eql 'The black leather trenchcoat you attempted ' \
30
+ expect(error.detail).to eql 'The black leather trenchcoat you attempted ' \
31
31
  'to bullet time for this request is either ' \
32
32
  'not authorized for the authenticated user ' \
33
33
  'or does not exist.'
34
34
  end
35
35
 
36
- it 'includes the resource name and action in the developer details' do
36
+ it 'includes the resource name and action in the source' do
37
37
  error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
38
38
  resource_id: 123
39
39
 
40
- expect(error.developer_details).to eql('black_leather_trenchcoat_id' => 123)
40
+ expect(error.source).to eql('black_leather_trenchcoat_id' => 123)
41
41
  end
42
42
 
43
43
  it 'can accept an array of IDs' do
44
44
  error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
45
45
  resource_id: %w{123 456}
46
46
 
47
- expect(error.developer_details).to eql('black_leather_trenchcoat_id' => %w{123 456})
48
- end
49
-
50
- it 'includes the resource name and action in the friendly message' do
51
- error = ResourceNotFoundError.new resource_name: 'black leather trenchcoat',
52
- action: 'bullet time'
53
-
54
- expect(error.friendly_message).to eql 'Sorry! The black leather trenchcoat you ' \
55
- 'tried to bullet time does not exist.'
47
+ expect(error.source).to eql('black_leather_trenchcoat_id' => %w{123 456})
56
48
  end
57
49
 
58
50
  it 'can convert an "ActiveRecord::RecordNotFound" with no IDs' do
@@ -29,41 +29,33 @@ describe ResourcePersistenceError do
29
29
  expect(error.http_status).to eql 422
30
30
  end
31
31
 
32
- it 'has a code of 1006' do
32
+ it 'has a code' do
33
33
  error = ResourcePersistenceError.new
34
34
 
35
- expect(error.code).to eql 1006
35
+ expect(error.code).to eql 'errors.resource_persistence_error'
36
36
  end
37
37
 
38
- it 'has a knowledgebase article ID of 1234567890' do
38
+ it 'has a title' do
39
39
  error = ResourcePersistenceError.new
40
40
 
41
- expect(error.knowledgebase_article_id).to eql '1234567890'
41
+ expect(error.title).to eql 'Resource Persistence Error'
42
42
  end
43
43
 
44
- it 'includes the resource name and action in the developer message' do
44
+ it 'includes the resource name and action in the detail' do
45
45
  error = ResourcePersistenceError.new resource_name: 'black leather trenchcoat',
46
46
  action: 'bullet time'
47
47
 
48
- expect(error.developer_message).to eql 'One or more of the attributes on the black ' \
48
+ expect(error.detail).to eql 'One or more of the attributes on the black ' \
49
49
  'leather trenchcoat you attempted to bullet ' \
50
50
  'time is invalid.'
51
51
  end
52
52
 
53
- it 'includes the resource name and action in the developer details' do
53
+ it 'includes the resource name and action in the source' do
54
54
  error = ResourcePersistenceError.new errors: 'lots of errors',
55
55
  attributes: 'what is the matrix'
56
56
 
57
- expect(error.developer_details).to eql('errors' => 'lots of errors',
58
- 'attributes' => 'what is the matrix')
59
- end
60
-
61
- it 'includes the resource name and action in the friendly message' do
62
- error = ResourcePersistenceError.new resource_name: 'black leather trenchcoat',
63
- action: 'bullet time'
64
-
65
- expect(error.friendly_message).to eql 'Sorry! We had a problem when tried to ' \
66
- 'bullet time that black leather trenchcoat.'
57
+ expect(error.source).to eql('errors' => 'lots of errors',
58
+ 'attributes' => 'what is the matrix')
67
59
  end
68
60
 
69
61
  it 'can convert an "ActiveRecord::RecordNotSaved"' do
@@ -0,0 +1,55 @@
1
+ require 'rspectacular'
2
+ require 'human_error'
3
+ require 'action_controller'
4
+
5
+ class HumanError
6
+ module Errors
7
+ describe ParameterMissingError do
8
+ it 'has a status' do
9
+ error = ParameterMissingError.new
10
+
11
+ expect(error.http_status).to eql 400
12
+ end
13
+
14
+ it 'has a code' do
15
+ error = ParameterMissingError.new
16
+
17
+ expect(error.code).to eql 'errors.parameter_missing_error'
18
+ end
19
+
20
+ it 'has a title' do
21
+ error = ParameterMissingError.new
22
+
23
+ expect(error.title).to eql 'Missing Parameter'
24
+ end
25
+
26
+ it 'includes the resource name and action in the detail' do
27
+ error = ParameterMissingError.new parameter: 'trenchcoat'
28
+
29
+ expect(error.detail).to eql 'trenchcoat is a required parameter, but you did not ' \
30
+ 'supply it.'
31
+ end
32
+
33
+ it 'includes the resource name and action in the source' do
34
+ error = ParameterMissingError.new parameter: 'trenchcoat'
35
+
36
+ expect(error.source).to eql('required_parameter' => 'trenchcoat')
37
+ end
38
+
39
+ it 'can convert an "ActionController::ParameterMissing"' do
40
+ parameter_missing_error = ActionController::ParameterMissing.new('trenchcoat')
41
+ error = ParameterMissingError.convert(parameter_missing_error)
42
+
43
+ expect(error.parameter).to eql 'trenchcoat'
44
+ end
45
+
46
+ it 'can convert an "ActionController::ParameterMissing" while overriding attributes' do
47
+ parameter_missing_error = ActionController::ParameterMissing.new('trenchcoat')
48
+ error = ParameterMissingError.convert(parameter_missing_error,
49
+ parameter: 'matrix')
50
+
51
+ expect(error.parameter).to eql 'matrix'
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,62 @@
1
+ require 'rspectacular'
2
+ require 'human_error'
3
+ require 'action_controller'
4
+
5
+ class HumanError
6
+ module Errors
7
+ describe UnpermittedParametersError do
8
+ it 'has a status' do
9
+ error = UnpermittedParametersError.new
10
+
11
+ expect(error.http_status).to eql 400
12
+ end
13
+
14
+ it 'has a code' do
15
+ error = UnpermittedParametersError.new
16
+
17
+ expect(error.code).to eql 'errors.unpermitted_parameters_error'
18
+ end
19
+
20
+ it 'has a title' do
21
+ error = UnpermittedParametersError.new
22
+
23
+ expect(error.title).to eql 'Unpermitted Parameters'
24
+ end
25
+
26
+ it 'includes the resource name and action in the detail' do
27
+ error = UnpermittedParametersError.new parameters: 'trenchcoat'
28
+
29
+ expect(error.detail).to eql 'The following parameters passed are not allowed: ' \
30
+ 'trenchcoat'
31
+ end
32
+
33
+ it 'includes the resource name and action in the detail' do
34
+ error = UnpermittedParametersError.new parameters: %w{trenchcoat matrix}
35
+
36
+ expect(error.detail).to eql 'The following parameters passed are not allowed: ' \
37
+ 'trenchcoat, matrix'
38
+ end
39
+
40
+ it 'includes the resource name and action in the source' do
41
+ error = UnpermittedParametersError.new parameters: 'trenchcoat'
42
+
43
+ expect(error.source).to eql('unpermitted_parameters' => ['trenchcoat'])
44
+ end
45
+
46
+ it 'can convert an "ActionController::UnpermittedParameters"' do
47
+ parameters_error = ActionController::UnpermittedParameters.new(%w{trenchcoat})
48
+ error = UnpermittedParametersError.convert(parameters_error)
49
+
50
+ expect(error.parameters).to eql %w{trenchcoat}
51
+ end
52
+
53
+ it 'can convert an "ActionController::ParameterMissing" while overriding attributes' do
54
+ parameters_error = ActionController::UnpermittedParameters.new(%w{trenchcoat})
55
+ error = UnpermittedParametersError.convert(parameters_error,
56
+ parameters: 'matrix')
57
+
58
+ expect(error.parameters).to eql %w{matrix}
59
+ end
60
+ end
61
+ end
62
+ end
@@ -7,66 +7,13 @@ describe HumanError do
7
7
  ActiveRecord::RecordNotFound.new("Couldn't find resource with 'id'=3")
8
8
  end
9
9
 
10
- it 'can create an instance of HumanError' do
11
- expect(HumanError.new).to be_a HumanError
12
- end
13
-
14
- it 'can configure each instance' do
15
- human_error = HumanError.new do |config|
16
- config.api_version = 'foo'
17
- end
18
-
19
- expect(human_error.configuration.api_version).to eql 'foo'
20
- end
21
-
22
10
  it 'can lookup errors' do
23
- human_error = HumanError.new
24
-
25
- expect(human_error.fetch('RequestError')).to eql HumanError::Errors::RequestError
26
- end
27
-
28
- it 'can lookup errors based on the local configuration' do
29
- human_error = HumanError.new do |config|
30
- config.api_version = 'foo'
31
- end
32
-
33
- fetched_error = human_error.convert(original_error)
34
-
35
- expect(fetched_error.api_version).to eql 'foo'
36
- end
37
-
38
- it 'can override values in the global configuration with values in the local' \
39
- 'configuration when looking up an error' do
40
-
41
- HumanError.configure do |config|
42
- config.api_version = 'bar'
43
- end
44
-
45
- human_error = HumanError.new do |config|
46
- config.api_version = 'foo'
47
- end
48
-
49
- fetched_error = human_error.convert(original_error)
50
-
51
- expect(fetched_error.api_version).to eql 'foo'
52
- end
53
-
54
- it 'can override values in the local configuration with explicit values passed when' \
55
- 'looking up an error' do
56
-
57
- human_error = HumanError.new do |config|
58
- config.api_version = 'foo'
59
- end
60
-
61
- fetched_error = human_error.convert(original_error, api_version: 'bar')
62
-
63
- expect(fetched_error.api_version).to eql 'bar'
11
+ expect(HumanError.fetch('InvalidTokenError')).to \
12
+ eql HumanError::Errors::InvalidTokenError
64
13
  end
65
14
 
66
15
  it 'can raise an error' do
67
- human_error = HumanError.new
68
-
69
- expect { human_error.raise('RequestError') }.to \
70
- raise_error HumanError::Errors::RequestError
16
+ expect { HumanError.raise('InvalidTokenError') }.to \
17
+ raise_error HumanError::Errors::InvalidTokenError
71
18
  end
72
19
  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: 2.0.0
4
+ version: 3.0.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-04-11 00:00:00.000000000 Z
11
+ date: 2015-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.50'
33
+ version: '0.64'
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
- version: '0.50'
40
+ version: '0.64'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '4.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: actionpack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.1'
55
69
  description: ''
56
70
  email: accounts+git@thekompanee.com
57
71
  executables: []
@@ -64,8 +78,6 @@ files:
64
78
  - lib/human_error.rb
65
79
  - lib/human_error/configuration.rb
66
80
  - lib/human_error/error.rb
67
- - lib/human_error/error_code_directory.rb
68
- - lib/human_error/errors.rb
69
81
  - lib/human_error/errors/authentication_error.rb
70
82
  - lib/human_error/errors/authentication_errors/duplicate_authentication_error.rb
71
83
  - lib/human_error/errors/authentication_errors/invalid_token_error.rb
@@ -74,11 +86,11 @@ files:
74
86
  - lib/human_error/errors/crud_errors/association_error.rb
75
87
  - lib/human_error/errors/crud_errors/resource_not_found_error.rb
76
88
  - lib/human_error/errors/crud_errors/resource_persistence_error.rb
77
- - lib/human_error/errors/request_error.rb
78
- - lib/human_error/knowledgebase_id_directory.rb
89
+ - lib/human_error/errors/request_errors/parameter_missing_error.rb
90
+ - lib/human_error/errors/request_errors/unpermitted_parameters_error.rb
79
91
  - lib/human_error/rescuable_resource.rb
80
92
  - lib/human_error/utilities/string.rb
81
- - lib/human_error/verifiable_model.rb
93
+ - lib/human_error/verifiable_resource.rb
82
94
  - lib/human_error/version.rb
83
95
  - spec/lib/human_error/configuration_spec.rb
84
96
  - spec/lib/human_error/error_spec.rb
@@ -88,7 +100,8 @@ files:
88
100
  - spec/lib/human_error/errors/crud_errors/association_error_spec.rb
89
101
  - spec/lib/human_error/errors/crud_errors/resource_not_found_error_spec.rb
90
102
  - spec/lib/human_error/errors/crud_errors/resource_persistence_error_spec.rb
91
- - spec/lib/human_error/errors/request_error_spec.rb
103
+ - spec/lib/human_error/errors/request_errors/parameter_missing_error_spec.rb
104
+ - spec/lib/human_error/errors/request_errors/unpermitted_parameters_error_spec.rb
92
105
  - spec/lib/human_error/rescuable_resource_spec.rb
93
106
  - spec/lib/human_error_spec.rb
94
107
  homepage: https://github.com/thekompanee/human_error
@@ -111,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
124
  version: '0'
112
125
  requirements: []
113
126
  rubyforge_project:
114
- rubygems_version: 2.4.6
127
+ rubygems_version: 2.4.8
115
128
  signing_key:
116
129
  specification_version: 4
117
130
  summary: Common Error Extensions and Helpers
@@ -124,6 +137,7 @@ test_files:
124
137
  - spec/lib/human_error/errors/crud_errors/association_error_spec.rb
125
138
  - spec/lib/human_error/errors/crud_errors/resource_not_found_error_spec.rb
126
139
  - spec/lib/human_error/errors/crud_errors/resource_persistence_error_spec.rb
127
- - spec/lib/human_error/errors/request_error_spec.rb
140
+ - spec/lib/human_error/errors/request_errors/parameter_missing_error_spec.rb
141
+ - spec/lib/human_error/errors/request_errors/unpermitted_parameters_error_spec.rb
128
142
  - spec/lib/human_error/rescuable_resource_spec.rb
129
143
  - spec/lib/human_error_spec.rb