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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +4 -1
- data/lib/jsonapi_errors_handler.rb +22 -25
- data/lib/jsonapi_errors_handler/error_mapper.rb +3 -1
- data/lib/jsonapi_errors_handler/{error_searializer.rb → error_serializer.rb} +0 -0
- data/lib/jsonapi_errors_handler/version.rb +1 -1
- metadata +3 -6
- data/jsonapi_errors_handler-0.0.0.gem +0 -0
- data/jsonapi_errors_handler-0.0.1.gem +0 -0
- data/jsonapi_errors_handler-0.0.2.gem +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e90ab4d7f467391ce6573301ba495731e91bacd53d5b8411b187264f634d020
|
4
|
+
data.tar.gz: 636457e3b271ef91cfd6e95fa2163fdd3bc781c4fce8dfc9cb504b37f13104c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '059874df7053302d90ef11f19500705fbbac5225c40276771e791981f480040753677127190d3d1e4a242b8e1ca8550086cd3ff93ba8d3d75dbd02738b1a19b3'
|
7
|
+
data.tar.gz: fe491f4940b164d5bb32573ca6fd453de9f7ea55e83d33f14ffd7255ec2e1310998afe7002bfb9cf1c60f09c836b95895f4a0c23a6e0593119a1fbcffc96a74f
|
data/Gemfile.lock
CHANGED
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
|
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
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
41
|
-
|
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
|
File without changes
|
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
|
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-
|
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/
|
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
|
Binary file
|
Binary file
|
Binary file
|