smart_error 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 2436ad75624ac387e6826270dba88d46c94ea938
4
- data.tar.gz: 0631c737389e4f733e917cbd3ad5dac8e41d890f
2
+ SHA256:
3
+ metadata.gz: 59d9a3f3b7d1095811041c456d368bc58d7eb72b686e25772f221923aaa7fc99
4
+ data.tar.gz: 537090f991ece72696eb0d13f6ed9158c8e4c4e6601557567d6e1aa29be7b163
5
5
  SHA512:
6
- metadata.gz: 6e7e05b1d4a7e19d55280d806dc0fd4d18eb1117e97364695c702badb0feafc39735dccb7187a268e24f20f3f88931fee0042fa06b0be20ad847404248abca64
7
- data.tar.gz: ac9a8e57269683ce1723be864b8cf7934de2df00770fe665de7a3e9362440c8f3654e98452afa421d86d032729af63cd0e18432a26c8740c3d4cd9c832785b9d
6
+ metadata.gz: eb6052bfe1dae0cbbd5b381207e1efdece8ec963b7b64eb9d88f4418505a3e78f81767beac2d4040a227fb7263b56bd521af53beb4721bc83ebbe45b9765cb49
7
+ data.tar.gz: '08731b218c5eabe0739eeeae5bde9c8fc69c7b4977a50ebf7608aafe79688b3d63141d8d5a77985137785b078c88411bf71d3a9845c625afaf468451fd2a6a2e'
@@ -0,0 +1,120 @@
1
+ # Usage: rubocop -a app/controllers/run/on/specific/file
2
+ require:
3
+ - rubocop-performance
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.7.1
7
+ Exclude:
8
+ - 'bin/**/*'
9
+
10
+ Layout/EmptyLinesAroundAttributeAccessor:
11
+ Enabled: true
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Layout/SpaceAroundMethodCallOperator:
17
+ Enabled: true
18
+
19
+ Layout/SpaceInLambdaLiteral:
20
+ Enabled: true
21
+
22
+ Layout/DotPosition:
23
+ Description: 'Checks the position of the dot in multi-line method calls.'
24
+ StyleGuide: '#consistent-multi-line-chains'
25
+ Enabled: true
26
+ EnforcedStyle: trailing
27
+
28
+ Lint/Void:
29
+ Enabled: false
30
+
31
+ Lint/DeprecatedOpenSSLConstant:
32
+ Enabled: true
33
+
34
+ Lint/MixedRegexpCaptureTypes:
35
+ Enabled: false
36
+
37
+ Lint/RaiseException:
38
+ Enabled: true
39
+
40
+ Lint/StructNewOverride:
41
+ Enabled: true
42
+
43
+ Style/CaseLikeIf:
44
+ Description: 'This cop identifies places where `if-elsif` constructions can be replaced with `case-when`.'
45
+ StyleGuide: '#case-vs-if-else'
46
+ Enabled: true
47
+
48
+ Style/EmptyCaseCondition:
49
+ Description: 'Avoid empty condition in case statements.'
50
+ Enabled: false
51
+
52
+ Style/HashEachMethods:
53
+ Enabled: true
54
+
55
+ Style/HashTransformValues:
56
+ Enabled: true
57
+
58
+ Style/Documentation:
59
+ Enabled: false
60
+
61
+ Style/ClassAndModuleChildren:
62
+ Enabled: false
63
+
64
+ Style/HashTransformKeys:
65
+ Enabled: true
66
+
67
+ Style/ExponentialNotation:
68
+ Enabled: true
69
+
70
+ Style/RedundantRegexpCharacterClass:
71
+ Enabled: true
72
+
73
+ Style/RedundantRegexpEscape:
74
+ Enabled: true
75
+
76
+ Style/SlicingWithRange:
77
+ Enabled: true
78
+
79
+ Style/Lambda:
80
+ EnforcedStyle: literal
81
+
82
+ Style/HashSyntax:
83
+ Enabled: true
84
+
85
+ Style/RedundantSelf:
86
+ Description: "Don't use self where it's not needed."
87
+ StyleGuide: '#no-self-unless-required'
88
+ Enabled: false
89
+
90
+ Style/MethodCallWithArgsParentheses:
91
+ Description: 'Use parentheses for method calls with arguments.'
92
+ StyleGuide: '#method-invocation-parens'
93
+ EnforcedStyle: require_parentheses
94
+ Enabled: false
95
+ Exclude:
96
+ - '**/*.gemfile'
97
+ - '**/Gemfile'
98
+ - '**/presenters/*.rb'
99
+ - '**/spec/*'
100
+
101
+
102
+ Style:
103
+ BlockDelimiters:
104
+ EnforcedStyle: semantic
105
+
106
+ Style/Documentation:
107
+ Description: 'Document classes and non-namespace modules.'
108
+ Enabled: false
109
+
110
+ Metrics/AbcSize:
111
+ Max: 30
112
+
113
+ Metrics/ClassLength:
114
+ Max: 1500
115
+
116
+ Metrics/MethodLength:
117
+ Max: 25
118
+
119
+ Metrics/BlockLength:
120
+ Max: 70
@@ -1 +1 @@
1
- 2.4.1
1
+ 2.7.2
@@ -2,4 +2,4 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.4.1
5
- before_install: gem install bundler -v 1.16.0
5
+ before_install: gem install bundler -v 1.17.3
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in smart_error.gemspec
6
6
  gemspec
data/README.md CHANGED
@@ -5,6 +5,29 @@
5
5
 
6
6
  Simple gem that can handle Exceptions, ActiveRecord Errors and Custom Errors and return readable message and error number/code as usable Hash.
7
7
 
8
+ <!-- START doctoc generated TOC please keep comment here to allow auto update -->
9
+ <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
10
+ **Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
11
+
12
+ - [SmartError](#smarterror)
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [Handle integer as CustomError](#handle-integer-as-customerror)
16
+ - [Handle ApplicationRecord as ModelError](#handle-applicationrecord-as-modelerror)
17
+ - [Handle ActiveRecord child as ModelError](#handle-activerecord-child-as-modelerror)
18
+ - [handle Exception as ExceptionError](#handle-exception-as-exceptionerror)
19
+ - [Override the message with the provided message](#override-the-message-with-the-provided-message)
20
+ - [Merge `extra` array to message](#merge-extra-array-to-message)
21
+ - [Merge `extra` string to message](#merge-extra-string-to-message)
22
+ - [Merge `extra` with custom message](#merge-extra-with-custom-message)
23
+ - [Show First Model Errors by default](#show-first-model-errors-by-default)
24
+ - [Show Model Error details by invalid keys and messages and convert messages to sentence](#show-model-error-details-by-invalid-keys-and-messages-and-convert-messages-to-sentence)
25
+ - [Contributing](#contributing)
26
+ - [License](#license)
27
+ - [Code of Conduct](#code-of-conduct)
28
+
29
+ <!-- END doctoc generated TOC please keep comment here to allow auto update -->
30
+
8
31
  ## Installation
9
32
 
10
33
  Add this line to your application’s Gemfile:
@@ -26,9 +49,81 @@ $ gem install smart_error
26
49
  ```
27
50
 
28
51
  ## Usage
52
+ First of all will need to have predefined list of error messages Like: [errors](https://github.com/ali-sheiba/smart_error/blob/master/spec/en.yml)
53
+
54
+
55
+ ### Handle integer as CustomError
56
+
57
+ ```ruby
58
+ SmartError.handle(1001).to_h
59
+ # {:message=>"Unauthorized", :details=>{}, :error_url=>"1001", :error_code=>1001}
60
+
61
+ SmartError.handle(1001).to_json
62
+ # "{\"message\":\"Unauthorized\",\"details\":{},\"error_url\":\"1001\",\"error_code\":1001}"
63
+
64
+ SmartError.handle(1001, options).as_json
65
+ # {"message"=>"Unauthorized", "details"=>{}, "error_url"=>"1001", "error_code"=>1001}
66
+ ```
67
+
68
+ ### Handle ApplicationRecord as ModelError
69
+ ```ruby
70
+ SmartError.handle(Employee.new).to_h
71
+ # {:message=>"", :details=>{}, :error_url=>"1010", :error_code=>1010}
72
+ ```
73
+
74
+ ### Handle ActiveRecord child as ModelError
75
+ ```ruby
76
+ SmartError.handle(ApplicationRecord.new).to_h
77
+ ```
78
+
79
+ ### handle Exception as ExceptionError
80
+ ```ruby
81
+ SmartError.handle(NoMethodError.new).to_h
82
+ # {:message=>"NoMethodError", :details=>{}, :error_url=>"1000", :error_code=>1000}
83
+ ```
84
+
85
+ ### Override the message with the provided message
86
+ ```ruby
87
+ message = 'Custom Message'
88
+ SmartError.handle(1001, message: message).to_h
89
+ # {:message=>"Custom Message", :details=>{}, :error_url=>"1001", :error_code=>1001}
90
+ ```
91
+
92
+ ### Merge `extra` array to message
93
+ ```ruby
94
+ extra = %i[username password country]
95
+ error = SmartError.handle(1002, extra: extra)
96
+ error.to_h
97
+ # {:message=>"Not Found: username, password, and country", :details=>{}, :error_url=>"1002", :error_code=>1002}
98
+ ```
99
+
100
+ ### Merge `extra` string to message
101
+ ```ruby
102
+ extra = 'you are noy authorized for this action'
103
+ error = SmartError.handle(1001, extra: extra)
104
+ error.to_h
105
+ # {:message=>"Unauthorized: you are noy authorized for this action", :details=>{}, :error_url=>"1001", :error_code=>1001}
106
+ ```
29
107
 
30
- WIP
108
+ ### Merge `extra` with custom message
109
+ ```ruby
110
+ extra = %i[username password country]
111
+ message = 'Missing Parameters'
112
+ error = SmartError.handle(1001, message: message, extra: extra)
113
+ error.to_h
114
+ # {:message=>"Missing Parameters: username, password, and country", :details=>{}, :error_url=>"1001", :error_code=>1001}
115
+ ```
31
116
 
117
+ ### Show First Model Errors by default
118
+ ```ruby
119
+ SmartError.handle(ExampleModel.new).to_h
120
+ ```
121
+ ### Show Model Error details by invalid keys and messages and convert messages to sentence
122
+ ```ruby
123
+ error = SmartError.handle(ExampleModel.new)
124
+ error.to_h[:details] # 'can\'t be blank'
125
+ error.to_h[:details] # 'can\'t be blank and is invalid'
126
+ ```
32
127
  ## Contributing
33
128
 
34
129
  Bug reports and pull requests are welcome on GitHub at <https://github.com/ali-sheiba/smart_error>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+
10
+ def last_commit_id_of_base_branch
11
+ `git log | sed -n 1p`.split(' ').last
12
+ end
13
+
14
+ puts "== Running rubocop with safe auto correct on modified files after #{last_commit_id_of_base_branch} commit =="
15
+ system("git diff #{last_commit_id_of_base_branch} --name-only | xargs bundle exec rubocop -A")
@@ -22,14 +22,15 @@ module SmartError
22
22
  @@model_error_message = :first
23
23
 
24
24
  def self.handle(error, options = nil)
25
- if error.is_a?(Integer)
25
+ case error
26
+ when Integer
26
27
  SmartError::CustomError.new(error, options)
27
- elsif error.is_a?(Exception)
28
+ when Exception
28
29
  SmartError::ExceptionError.new(error, options)
29
- elsif error.is_a?(ApplicationRecord) || error.is_a?(ActiveRecord)
30
+ when ApplicationRecord, ActiveRecord
30
31
  SmartError::ModelError.new(error, options)
31
32
  else
32
- SmartError::CustomeError.new(1000, options)
33
+ SmartError::CustomError.new(1000, options)
33
34
  end
34
35
  end
35
36
 
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartError
4
- # THe Base Class of Errors
4
+ # The Base Class of Errors
5
5
  class BaseError
6
6
  def initialize(error, options = {})
7
7
  @error = error
8
8
  @options = options&.symbolize_keys! || {}
9
9
  end
10
10
 
11
- def message
11
+ # :nocov:
12
+ def not_implemented
12
13
  raise NotImplementedError
13
14
  end
14
15
 
15
- def error_code
16
- raise NotImplementedError
17
- end
16
+ alias message not_implemented
17
+ alias error_code not_implemented
18
+ # :nocov:
18
19
 
19
20
  def details
20
21
  {}
@@ -7,18 +7,24 @@ module SmartError
7
7
  end
8
8
 
9
9
  def message
10
- action = case SmartError.model_error_message
11
- when :first then :first
12
- when :sentence then :to_sentence
13
- when :last then :last
14
- else :first
15
- end
16
-
17
- @error.errors.full_messages.send(action)
10
+ @error.
11
+ errors.
12
+ full_messages.
13
+ send(
14
+ action[SmartError.model_error_message] || :first
15
+ )
18
16
  end
19
17
 
20
18
  def details
21
19
  @error.errors.messages.transform_values(&:to_sentence)
22
20
  end
21
+
22
+ def action
23
+ {
24
+ first: :first,
25
+ last: :last,
26
+ sentence: :to_sentence
27
+ }
28
+ end
23
29
  end
24
30
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartError
4
- VERSION = '1.0.2'
4
+ VERSION = '1.0.3'
5
5
  end
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'smart_error/version'
6
6
 
7
7
  Gem::Specification.new do |spec|
8
8
  spec.name = 'smart_error'
9
9
  spec.version = SmartError::VERSION
10
- spec.authors = ['Ali Al-Sheiba']
11
- spec.email = ['ali.alsheiba@gmail.com']
10
+ spec.authors = ['Ali Al-Sheiba', 'MajedBojan']
11
+ spec.email = ['ali.alsheiba@gmail.com', 'bojanmajed@gmail.com']
12
12
 
13
13
  spec.summary = 'Simple way to handle custom error codes and messages'
14
14
  spec.description = 'Simple way to maintain custom error codes and there messages with I18n.'
@@ -23,10 +23,12 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ['lib']
25
25
 
26
- spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_dependency 'i18n', '~> 1.8', '>= 1.8.5'
27
+ spec.add_dependency 'activesupport', '>= 5.0'
28
+ spec.add_development_dependency 'bundler', '~> 2.1'
27
29
  spec.add_development_dependency 'pry', '~> 0.11.3'
28
- spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rake', '~> 12.3'
29
31
  spec.add_development_dependency 'rspec', '~> 3.6'
30
- spec.add_dependency 'activesupport', '>= 5.0'
31
- spec.add_dependency 'i18n', '>= 0.8'
32
+ spec.add_development_dependency 'rubocop', '~> 0.93.1'
33
+ spec.add_development_dependency 'rubocop-performance', '~> 1.8', '>= 1.8.1'
32
34
  end
metadata CHANGED
@@ -1,29 +1,64 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_error
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ali Al-Sheiba
8
+ - MajedBojan
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2018-06-18 00:00:00.000000000 Z
12
+ date: 2020-10-21 00:00:00.000000000 Z
12
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.8'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.5
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '1.8'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.8.5
34
+ - !ruby/object:Gem::Dependency
35
+ name: activesupport
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ type: :runtime
42
+ prerelease: false
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
13
48
  - !ruby/object:Gem::Dependency
14
49
  name: bundler
15
50
  requirement: !ruby/object:Gem::Requirement
16
51
  requirements:
17
52
  - - "~>"
18
53
  - !ruby/object:Gem::Version
19
- version: '1.16'
54
+ version: '2.1'
20
55
  type: :development
21
56
  prerelease: false
22
57
  version_requirements: !ruby/object:Gem::Requirement
23
58
  requirements:
24
59
  - - "~>"
25
60
  - !ruby/object:Gem::Version
26
- version: '1.16'
61
+ version: '2.1'
27
62
  - !ruby/object:Gem::Dependency
28
63
  name: pry
29
64
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +79,14 @@ dependencies:
44
79
  requirements:
45
80
  - - "~>"
46
81
  - !ruby/object:Gem::Version
47
- version: '10.0'
82
+ version: '12.3'
48
83
  type: :development
49
84
  prerelease: false
50
85
  version_requirements: !ruby/object:Gem::Requirement
51
86
  requirements:
52
87
  - - "~>"
53
88
  - !ruby/object:Gem::Version
54
- version: '10.0'
89
+ version: '12.3'
55
90
  - !ruby/object:Gem::Dependency
56
91
  name: rspec
57
92
  requirement: !ruby/object:Gem::Requirement
@@ -67,42 +102,50 @@ dependencies:
67
102
  - !ruby/object:Gem::Version
68
103
  version: '3.6'
69
104
  - !ruby/object:Gem::Dependency
70
- name: activesupport
105
+ name: rubocop
71
106
  requirement: !ruby/object:Gem::Requirement
72
107
  requirements:
73
- - - ">="
108
+ - - "~>"
74
109
  - !ruby/object:Gem::Version
75
- version: '5.0'
76
- type: :runtime
110
+ version: 0.93.1
111
+ type: :development
77
112
  prerelease: false
78
113
  version_requirements: !ruby/object:Gem::Requirement
79
114
  requirements:
80
- - - ">="
115
+ - - "~>"
81
116
  - !ruby/object:Gem::Version
82
- version: '5.0'
117
+ version: 0.93.1
83
118
  - !ruby/object:Gem::Dependency
84
- name: i18n
119
+ name: rubocop-performance
85
120
  requirement: !ruby/object:Gem::Requirement
86
121
  requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.8'
87
125
  - - ">="
88
126
  - !ruby/object:Gem::Version
89
- version: '0.8'
90
- type: :runtime
127
+ version: 1.8.1
128
+ type: :development
91
129
  prerelease: false
92
130
  version_requirements: !ruby/object:Gem::Requirement
93
131
  requirements:
132
+ - - "~>"
133
+ - !ruby/object:Gem::Version
134
+ version: '1.8'
94
135
  - - ">="
95
136
  - !ruby/object:Gem::Version
96
- version: '0.8'
137
+ version: 1.8.1
97
138
  description: Simple way to maintain custom error codes and there messages with I18n.
98
139
  email:
99
140
  - ali.alsheiba@gmail.com
141
+ - bojanmajed@gmail.com
100
142
  executables: []
101
143
  extensions: []
102
144
  extra_rdoc_files: []
103
145
  files:
104
146
  - ".gitignore"
105
147
  - ".rspec"
148
+ - ".rubocop.yml"
106
149
  - ".ruby-gemset"
107
150
  - ".ruby-version"
108
151
  - ".travis.yml"
@@ -113,6 +156,7 @@ files:
113
156
  - README.md
114
157
  - Rakefile
115
158
  - bin/console
159
+ - bin/rubocop
116
160
  - bin/setup
117
161
  - lib/smart_error.rb
118
162
  - lib/smart_error/base_error.rb
@@ -120,7 +164,6 @@ files:
120
164
  - lib/smart_error/exception_error.rb
121
165
  - lib/smart_error/model_error.rb
122
166
  - lib/smart_error/version.rb
123
- - smart_error-1.0.1.gem
124
167
  - smart_error.gemspec
125
168
  homepage: https://github.com/ali-sheiba/smart_error
126
169
  licenses:
@@ -141,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
184
  - !ruby/object:Gem::Version
142
185
  version: '0'
143
186
  requirements: []
144
- rubyforge_project:
145
- rubygems_version: 2.6.13
187
+ rubygems_version: 3.1.4
146
188
  signing_key:
147
189
  specification_version: 4
148
190
  summary: Simple way to handle custom error codes and messages
Binary file