jsonapi_errors_handler 0.1.6 → 0.3.0

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
2
  SHA256:
3
- metadata.gz: ba2a86c74563398a7436e21ceea674a26a9672bbfba6c8fadc125df8eed70615
4
- data.tar.gz: f56a1281368f6ef3ab4c3a2a38afd1c80affd3997c32acca0f5cfe0784b4ba95
3
+ metadata.gz: bc11d29415335097f13316a8d7131068d2fbae08555f2a41d4962373dc9df472
4
+ data.tar.gz: 872131e2bda801b17c18bf670f25fde7550d74a3f58b91d919ea74c803848bf3
5
5
  SHA512:
6
- metadata.gz: 3661da30d706ed7cdc762136424326c0bca8d749afee213f80479e420d7f639bc1be1ef39954fced7ebf01c2a7a25aa8358dfe9ef5cc38ffb63f2f2984e4da86
7
- data.tar.gz: 62f72aa1e1ea8df5b8b68733a14cd3df02705fa47bd4b5c2f1dc5bdc66ef9f3ceb376473608d3927a9eb0669f569aa43e290e7cdc3011635eee35f038510933c
6
+ metadata.gz: 4dcc44875ddf60f2ecfb5cb039fecafb9e1a60d86e94e3f23432f149d294c9a3b7d61ec1cd4f623cff2197b5a5757788a23e50ba0396543cc0f33a74227f876b
7
+ data.tar.gz: 93b589ff1d2cc44518c6f74a6438de1eb0d53440007bce1356c394219baa5bd50f77ea4d7ce56e076c98a836173080caeb68b46143bff3a83ff2f6e6252505c5
@@ -0,0 +1,31 @@
1
+ name: Test & Release
2
+
3
+ on:
4
+ push:
5
+ branches: [ release ]
6
+
7
+ jobs:
8
+ build:
9
+ name: Build + Publish
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby 2.7.1
15
+ uses: actions/setup-ruby@v1
16
+ with:
17
+ ruby-version: 2.7.1
18
+ - name: Run tests
19
+ run: |
20
+ bundle install
21
+ bundle exec rspec
22
+ - name: Publish to RubyGems
23
+ run: |
24
+ mkdir -p $HOME/.gem
25
+ touch $HOME/.gem/credentials
26
+ chmod 0600 $HOME/.gem/credentials
27
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
28
+ gem build *.gemspec
29
+ gem push *.gem
30
+ env:
31
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,29 @@
1
+ name: Run tests
2
+
3
+ on:
4
+ push:
5
+ branches-ignore: [ release ]
6
+ pull_request:
7
+ branches: [ master release ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Test
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby 2.7.1
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.7.1
20
+ - name: Run tests
21
+ run: |
22
+ bundle install
23
+ CI=true bundle exec rspec
24
+ - name: Upload report
25
+ run: |
26
+ mv ./coverage/coverage.xml cobertura.xml
27
+ bash <(curl -Ls https://coverage.codacy.com/get.sh) report
28
+ env:
29
+ CODACY_PROJECT_TOKEN: ${{secrets.CODACY_PROJECT_TOKEN}}
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4.0
3
+
4
+ Metrics/BlockLength:
5
+ Exclude:
6
+ - "spec/**/*_spec.rb"
7
+ - "jsonapi_errors_handler.gemspec"
8
+
9
+ Metrics/ModuleLength:
10
+ Exclude:
11
+ - "spec/**/*_spec.rb"
@@ -1 +1 @@
1
- 2.6.3
1
+ 2.7.1
@@ -1,21 +1,29 @@
1
1
  ## Contributing
2
2
 
3
- Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
3
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/driggl/jsonapi_errors_handler>.
4
4
 
5
5
  **How to contribute:**
6
6
 
7
- 1. Create Issue on Github.
8
- 2. Fork repository
9
- 3. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
10
- 4. Commit changes
11
- - Keep commits small and atomic
12
- - Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
13
- - Keep commits imperative style
14
- - Mention issue number (i.e "Resolves: #3")
15
- 5. Make sure you cover your change with tests.
16
- 6. Create Pull Request
7
+ 1. Create Issue on Github.
8
+
9
+ 2. Fork repository
10
+
11
+ 3. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
12
+
13
+ 4. Commit changes
14
+ * Keep commits small and atomic
15
+
16
+ * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
17
+
18
+ * Keep commits imperative style
19
+
20
+ * Mention issue number (i.e "Resolves: #3")
21
+
22
+ 5. Make sure you cover your change with tests.
23
+
24
+ 6. Create Pull Request
17
25
 
18
26
  **Coding Guidelines**
19
27
 
20
- - We use [Rubocop](https://github.com/rubocop-hq/rubocop) and Ruby style guides for linting `*.rb` files.
21
- - We use [Remark Lint](https://github.com/remarkjs/remark-lint) for linting `*.md` files.
28
+ * We use [Rubocop](https://github.com/rubocop*hq/rubocop) and Ruby style guides for linting `*.rb` files.
29
+ * We use [Remark Lint](https://github.com/remarkjs/remark-lint) for linting `*.md` files.
@@ -1,35 +1,44 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi_errors_handler (0.1.5)
4
+ jsonapi_errors_handler (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.3)
10
- rake (10.5.0)
11
- rspec (3.8.0)
12
- rspec-core (~> 3.8.0)
13
- rspec-expectations (~> 3.8.0)
14
- rspec-mocks (~> 3.8.0)
15
- rspec-core (3.8.0)
16
- rspec-support (~> 3.8.0)
17
- rspec-expectations (3.8.2)
9
+ diff-lcs (1.4.4)
10
+ docile (1.3.2)
11
+ rake (13.0.1)
12
+ rspec (3.9.0)
13
+ rspec-core (~> 3.9.0)
14
+ rspec-expectations (~> 3.9.0)
15
+ rspec-mocks (~> 3.9.0)
16
+ rspec-core (3.9.2)
17
+ rspec-support (~> 3.9.3)
18
+ rspec-expectations (3.9.2)
18
19
  diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.8.0)
20
- rspec-mocks (3.8.0)
20
+ rspec-support (~> 3.9.0)
21
+ rspec-mocks (3.9.1)
21
22
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.8.0)
23
- rspec-support (3.8.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-support (3.9.3)
25
+ simplecov (0.18.5)
26
+ docile (~> 1.1)
27
+ simplecov-html (~> 0.11)
28
+ simplecov-cobertura (1.4.0)
29
+ simplecov (~> 0.8)
30
+ simplecov-html (0.12.2)
24
31
 
25
32
  PLATFORMS
26
33
  ruby
27
34
 
28
35
  DEPENDENCIES
29
- bundler (~> 1.16)
36
+ bundler
30
37
  jsonapi_errors_handler!
31
- rake (~> 10.0)
32
- rspec (~> 3.0)
38
+ rake
39
+ rspec
40
+ simplecov
41
+ simplecov-cobertura
33
42
 
34
43
  BUNDLED WITH
35
- 1.17.3
44
+ 2.1.4
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- [![Codacy Badge](https://api.codacy.com/project/badge/Grade/542259a14a8f4b2894a39850c5031ffa)](https://app.codacy.com/app/swilgosz/jsonapi_errors_handler?utm_source=github.com&utm_medium=referral&utm_content=driggl/jsonapi_errors_handler&utm_campaign=Badge_Grade_Dashboard)
2
1
  [![Gem Version](https://badge.fury.io/rb/jsonapi_errors_handler.svg)](https://badge.fury.io/rb/jsonapi_errors_handler)
3
- [![CircleCI](https://circleci.com/gh/driggl/jsonapi_errors_handler/tree/master.svg?style=svg)](https://circleci.com/gh/driggl/jsonapi_errors_handler/tree/master)
2
+ [![Codacy Badge](https://app.codacy.com/project/badge/Grade/6dba6e4556354789809185e0decde4f8)](https://www.codacy.com/gh/driggl/jsonapi_errors_handler?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=driggl/jsonapi_errors_handler&amp;utm_campaign=Badge_Grade)
3
+ [![Codacy Badge](https://app.codacy.com/project/badge/Coverage/6dba6e4556354789809185e0decde4f8)](https://www.codacy.com/gh/driggl/jsonapi_errors_handler?utm_source=github.com&utm_medium=referral&utm_content=driggl/jsonapi_errors_handler&utm_campaign=Badge_Coverage)
4
+ ![Run tests](https://github.com/driggl/jsonapi_errors_handler/workflows/Run%20tests/badge.svg?branch=master)
4
5
 
5
6
  # JsonapiErrorsHandler
6
7
 
@@ -16,11 +17,15 @@ Add this line to your application's Gemfile:
16
17
 
17
18
  And then execute:
18
19
 
19
- $ bundle
20
+ ```shell
21
+ $ bundle
22
+ ```
20
23
 
21
24
  Or install it yourself as:
22
25
 
23
- $ gem install jsonapi_errors_handler
26
+ ```shell
27
+ $ gem install jsonapi_errors_handler
28
+ ```
24
29
 
25
30
  ## Usage
26
31
 
@@ -36,10 +41,22 @@ From this point you'll have default html errors being serialized. JsonapiErrorsH
36
41
  * [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
37
42
  * [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
38
43
  * [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
39
- * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
44
+ * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/unauthorized.rb)
40
45
 
41
46
  If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
42
47
 
48
+ ### Handling unexpected errors
49
+
50
+ If you want to handle all the errors in your API application to deliver nicely formatted JSON response about 500 instead crashing the server, add this when your application loads:
51
+
52
+ ```ruby
53
+ require 'jsonapi_errors_handler'
54
+
55
+ JsonapiErrorsHandler.configure do |config|
56
+ config.handle_unexpected = true
57
+ end
58
+ ```
59
+
43
60
  ### Custom errors mapping
44
61
 
45
62
  If you want your custom errors being handled by default, just add them to the mapper
@@ -47,13 +64,28 @@ If you want your custom errors being handled by default, just add them to the ma
47
64
  ```ruby
48
65
  include JsonapiErrorsHandler
49
66
  ErrorMapper.map_errors!({
50
- 'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
51
- 'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
67
+ 'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound'
52
68
  })
53
69
  rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
54
70
  ```
55
71
 
56
- ###Custom error logging
72
+ ### Handling rails-specific validation errors
73
+
74
+ To handle validation errors from ActiveRecord or ActiveModel, you need to write custom
75
+ error handler:
76
+
77
+ ```ruby
78
+ rescue_from ActiveRecord::RecordInvalid, with: lambda { |e| handle_validation_error(e) }
79
+ rescue_from ActiveModel::ValidationError, with: lambda { |e| handle_validation_error(e) }
80
+
81
+ def handle_validation_error(error)
82
+ error_model = error.try(:model) || error.try(:record)
83
+ mapped = JsonapiErrorsHandler::Errors::Invalid.new(errors: error_model.errors)
84
+ render_error(mapped)
85
+ end
86
+ ```
87
+
88
+ ### Custom error logging
57
89
 
58
90
  When you'll include the `jsonapi_errors_handler` to your controller, all errors will be handled and delivered to the client in the nice, formatted
59
91
  way.
@@ -76,7 +108,7 @@ By default, we deliver hardcoded responses. You can check out the defined error
76
108
  * [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
77
109
  * [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
78
110
  * [JsonapiErrorsHandler::Errors::NotFound](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
79
- * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/not_found.rb)
111
+ * [JsonapiErrorsHandler::Errors::Unauthorized](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/unauthorized.rb)
80
112
 
81
113
  If you want to have custom error responses being delivered, just create your own `Api::Errors` that inherits from `JsonapiErrorsHandler::StandardError`
82
114
 
@@ -112,16 +144,17 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
112
144
 
113
145
  ## Contributing
114
146
 
115
- Bug reports and pull requests are welcome on GitHub at [https://github.com/driggl/jsonapi_errors_handler](https://github.com/driggl/jsonapi_errors_handler).
147
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/driggl/jsonapi_errors_handler>.
116
148
 
117
149
  **How to contribute:**
118
150
 
119
151
  1. Fork repository
120
152
  2. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
121
153
  3. Commit changes
122
- * Keep commits small and atomic
123
- * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
124
- * Keep commits imperative style
154
+ * Keep commits small and atomic
155
+ * Start commit message from keywords (Add/Remove/Change/Refactor/Move/Rename/Upgrade/Downgrade)
156
+ * Keep commits imperative style
157
+
125
158
  4. Create Pull Request
126
159
 
127
160
  ## License
@@ -2,6 +2,7 @@
2
2
 
3
3
  lib = File.expand_path('lib', __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
5
6
  require 'jsonapi_errors_handler/version'
6
7
 
7
8
  Gem::Specification.new do |spec|
@@ -31,6 +32,8 @@ Gem::Specification.new do |spec|
31
32
  'public gem pushes.'
32
33
  end
33
34
 
35
+ spec.required_ruby_version = '>= 2.4.0'
36
+
34
37
  # Specify which files should be added to the gem when it is released.
35
38
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
36
39
  spec.files = Dir.chdir(File.expand_path(__dir__)) do
@@ -40,7 +43,9 @@ Gem::Specification.new do |spec|
40
43
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
44
  spec.require_paths = ['lib']
42
45
 
43
- spec.add_development_dependency 'bundler', '~> 1.16'
44
- spec.add_development_dependency 'rake', '~> 10.0'
45
- spec.add_development_dependency 'rspec', '~> 3.0'
46
+ spec.add_development_dependency 'bundler'
47
+ spec.add_development_dependency 'rake'
48
+ spec.add_development_dependency 'rspec'
49
+ spec.add_development_dependency 'simplecov'
50
+ spec.add_development_dependency 'simplecov-cobertura'
46
51
  end
@@ -1,21 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'irb'
4
3
  require 'jsonapi_errors_handler/version'
5
4
  require 'jsonapi_errors_handler/configuration'
6
5
  require 'jsonapi_errors_handler/errors'
7
6
  require 'jsonapi_errors_handler/error_mapper'
8
7
  require 'jsonapi_errors_handler/error_serializer'
9
8
 
9
+ # Allows to handle ruby errors and return the serialized JSON:API output
10
+ #
10
11
  module JsonapiErrorsHandler
12
+ PREDEFINED_HASH = {
13
+ 'JsonapiErrorsHandler::Errors::Invalid' =>
14
+ 'JsonapiErrorsHandler::Errors::Invalid',
15
+ 'JsonapiErrorsHandler::Errors::Forbidden' =>
16
+ 'JsonapiErrorsHandler::Errors::Forbidden',
17
+ 'JsonapiErrorsHandler::Errors::NotFound' => '
18
+ JsonapiErrorsHandler::Errors::NotFound',
19
+ 'JsonapiErrorsHandler::Errors::Unauthorized' =>
20
+ 'JsonapiErrorsHandler::Errors::Unauthorized'
21
+ }.freeze
22
+
11
23
  def self.included(base)
12
24
  base.class_eval do
13
- ErrorMapper.map_errors!(
14
- 'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
15
- 'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
16
- 'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
17
- 'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
18
- )
25
+ ErrorMapper.map_errors!(PREDEFINED_HASH)
19
26
  end
20
27
  end
21
28
 
@@ -25,7 +32,8 @@ module JsonapiErrorsHandler
25
32
  end
26
33
 
27
34
  def handle_unexpected_error(error)
28
- return raise error unless config.handle_unexpected?
35
+ return raise error unless configuration.handle_unexpected?
36
+
29
37
  log_error(error) if respond_to?(:log_error)
30
38
  render_error(::JsonapiErrorsHandler::Errors::StandardError.new)
31
39
  end
@@ -35,11 +43,10 @@ module JsonapiErrorsHandler
35
43
  end
36
44
 
37
45
  def self.configure(&block)
38
- config = Configuration.instance
39
- config.configure(&block)
46
+ Configuration.instance.configure(&block)
40
47
  end
41
48
 
42
- def self.config
49
+ def configuration
43
50
  Configuration.instance
44
51
  end
45
52
  end
@@ -3,15 +3,23 @@
3
3
  require 'singleton'
4
4
 
5
5
  module JsonapiErrorsHandler
6
+ # Configuration class allowing to set up the initial behavior for the gem
7
+ #
6
8
  class Configuration
7
9
  include Singleton
8
10
 
9
11
  attr_writer :handle_unexpected
10
12
 
13
+ # Allows to override the configuration options
14
+ # @param [Block] - list of options to be overwriten
15
+ #
11
16
  def configure
12
17
  yield(self) if block_given?
13
18
  end
14
19
 
20
+ # Checks if unexpected errors should be handled by the gem
21
+ # @return [Boolean]
22
+ #
15
23
  def handle_unexpected?
16
24
  @handle_unexpected
17
25
  end
@@ -1,14 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonapiErrorsHandler
4
+ # Maps any of the given error classes into the serializable errors
5
+ # from the predefined collection
6
+ #
4
7
  class ErrorMapper
5
- @@mapped_errors = {}
6
- def self.mapped_errors
7
- @@mapped_errors
8
+ @mapped_errors = {}
9
+
10
+ class << self
11
+ attr_reader :mapped_errors
8
12
  end
9
13
 
10
14
  def self.map_errors!(errors_hash = {})
11
- @@mapped_errors.merge!(errors_hash)
15
+ @mapped_errors.merge!(errors_hash)
12
16
  end
13
17
 
14
18
  def self.mapped_error?(error_klass)
@@ -22,11 +26,13 @@ module JsonapiErrorsHandler
22
26
  root_class = error_class.split('::').first
23
27
  mapped = mapped_errors[error_class] || mapped_errors[root_class]
24
28
  return unless mapped
29
+
25
30
  Object.const_get(mapped).new
26
31
  end
27
32
 
28
33
  def self.descendant_of_predefined?(error)
29
34
  return false if error.is_a?(Class)
35
+
30
36
  error.class < JsonapiErrorsHandler::Errors::StandardError
31
37
  end
32
38
  end