jsonapi_errors_handler 0.0.3 → 0.1.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: ae6f88da6bfca6bebd8417925b2226e1e8a649786a6f2a1eb0a0bce12c0987c0
4
- data.tar.gz: 8e7d4f693976c09d2141cd63029021bda76f17b5aa3af49c966911465bc7fdaf
3
+ metadata.gz: 3e90ab4d7f467391ce6573301ba495731e91bacd53d5b8411b187264f634d020
4
+ data.tar.gz: 636457e3b271ef91cfd6e95fa2163fdd3bc781c4fce8dfc9cb504b37f13104c7
5
5
  SHA512:
6
- metadata.gz: 945525e88fa59d012d4ee221c858b87f24bd40bec1cf156c32dbf6a97a29589bff94f14c76dab58049eec21359dcbecfef801402f6e7034e23246a24a4844fc5
7
- data.tar.gz: 570297676c549cadab03a25b7098ac3e4fdffab9a2a38de91279bc90de02ef583175aee06aa1d364c4e566b8ef4543d8ef2d64dde3c6e50330b2ead6175fa34d
6
+ metadata.gz: '059874df7053302d90ef11f19500705fbbac5225c40276771e791981f480040753677127190d3d1e4a242b8e1ca8550086cd3ff93ba8d3d75dbd02738b1a19b3'
7
+ data.tar.gz: fe491f4940b164d5bb32573ca6fd453de9f7ea55e83d33f14ffd7255ec2e1310998afe7002bfb9cf1c60f09c836b95895f4a0c23a6e0593119a1fbcffc96a74f
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi_errors_handler (0.0.0)
4
+ jsonapi_errors_handler (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -24,6 +24,7 @@ In your controller:
24
24
 
25
25
  ```
26
26
  include JsonapiErrorsHandler
27
+ rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
27
28
  ```
28
29
 
29
30
  From this point you'll have default html errors being serialized. JsonapiErrorsHandler offers 4 predefined errors:
@@ -36,13 +37,15 @@ If you rise any of errors above in any place of your application, client gets th
36
37
 
37
38
  ### Custom errors mapping
38
39
 
39
- If you want your custom errors being handled by default, just add them to th emapper
40
+ If you want your custom errors being handled by default, just add them to the mapper
40
41
 
41
42
  ```
43
+ include JsonapiErrorsHandler
42
44
  ErrorsMapper.map_errors!({
43
45
  'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
44
46
  'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
45
47
  })
48
+ rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
46
49
  ```
47
50
 
48
51
  ## Development
@@ -1,44 +1,41 @@
1
+ require 'irb'
1
2
  require "jsonapi_errors_handler/version"
3
+ require "jsonapi_errors_handler/errors"
2
4
  require "jsonapi_errors_handler/error_mapper"
3
5
  require "jsonapi_errors_handler/error_serializer"
4
6
 
5
7
  module JsonapiErrorsHandler
6
8
  def self.included(base)
7
- base.extend ClassMethods
8
9
  base.class_eval do
9
- ErrorsMapper.map_errors!({
10
+ ErrorMapper.map_errors!({
10
11
  'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
11
12
  'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
12
13
  'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
13
14
  'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
14
15
  })
15
16
 
16
- rescue_from(::StandardError, with: lambda { |e| handle_error(e) })
17
- end
18
- end
19
-
20
- module ClassMethods
21
- def handle_error(e)
22
- mapped = map_error(e)
23
- unless Rails.env.development?
24
- # notify about unexpected_error unless mapped
25
- mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
26
- end
27
- if mapped
28
- render_error(mapped)
29
- else
30
- raise e
17
+ def handle_error(e)
18
+ mapped = map_error(e)
19
+ unless Rails.env.development?
20
+ # notify about unexpected_error unless mapped
21
+ mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
22
+ end
23
+ if mapped
24
+ render_error(mapped)
25
+ else
26
+ raise e
27
+ end
31
28
  end
32
- end
33
29
 
34
- def map_error(e)
35
- error_klass = e.class.name
36
- return e if ErrorsMapper.mapped_error?(error_klass)
37
- ErrorsMapper.mapped_errors[error_klass]&.constantize&.new
38
- end
30
+ def map_error(e)
31
+ error_klass = e.class.name
32
+ return e if ErrorMapper.mapped_error?(error_klass)
33
+ ErrorMapper.mapped_errors[error_klass]&.constantize&.new
34
+ end
39
35
 
40
- def render_error(error)
41
- render json: ::JsonapiErrorHandler::ErrorSerializer.new(error), status: error.status
36
+ def render_error(error)
37
+ render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
38
+ end
42
39
  end
43
40
  end
44
41
  end
@@ -1,6 +1,8 @@
1
1
  module JsonapiErrorsHandler
2
2
  class ErrorMapper
3
- cattr_accessor :mapped_errors
3
+ def self.mapped_errors
4
+ @@mapped_errors
5
+ end
4
6
 
5
7
  def self.map_errors!(errors_hash={})
6
8
  @@mapped_errors ||= {}
@@ -1,3 +1,3 @@
1
1
  module JsonapiErrorsHandler
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_errors_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Wilgosz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-03-26 00:00:00.000000000 Z
11
+ date: 2019-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,13 +70,10 @@ files:
70
70
  - Rakefile
71
71
  - bin/console
72
72
  - bin/setup
73
- - jsonapi_errors_handler-0.0.0.gem
74
- - jsonapi_errors_handler-0.0.1.gem
75
- - jsonapi_errors_handler-0.0.2.gem
76
73
  - jsonapi_errors_handler.gemspec
77
74
  - lib/jsonapi_errors_handler.rb
78
75
  - lib/jsonapi_errors_handler/error_mapper.rb
79
- - lib/jsonapi_errors_handler/error_searializer.rb
76
+ - lib/jsonapi_errors_handler/error_serializer.rb
80
77
  - lib/jsonapi_errors_handler/errors.rb
81
78
  - lib/jsonapi_errors_handler/errors/forbidden.rb
82
79
  - lib/jsonapi_errors_handler/errors/invalid.rb