jsonapi_errors_handler 0.1.5 → 0.1.6
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 +1 -1
- data/lib/jsonapi_errors_handler.rb +16 -11
- data/lib/jsonapi_errors_handler/configuration.rb +25 -0
- data/lib/jsonapi_errors_handler/error_mapper.rb +15 -0
- data/lib/jsonapi_errors_handler/errors/standard_error.rb +1 -1
- data/lib/jsonapi_errors_handler/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ba2a86c74563398a7436e21ceea674a26a9672bbfba6c8fadc125df8eed70615
|
|
4
|
+
data.tar.gz: f56a1281368f6ef3ab4c3a2a38afd1c80affd3997c32acca0f5cfe0784b4ba95
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3661da30d706ed7cdc762136424326c0bca8d749afee213f80479e420d7f639bc1be1ef39954fced7ebf01c2a7a25aa8358dfe9ef5cc38ffb63f2f2984e4da86
|
|
7
|
+
data.tar.gz: 62f72aa1e1ea8df5b8b68733a14cd3df02705fa47bd4b5c2f1dc5bdc66ef9f3ceb376473608d3927a9eb0669f569aa43e290e7cdc3011635eee35f038510933c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -46,7 +46,7 @@ If you want your custom errors being handled by default, just add them to the ma
|
|
|
46
46
|
|
|
47
47
|
```ruby
|
|
48
48
|
include JsonapiErrorsHandler
|
|
49
|
-
|
|
49
|
+
ErrorMapper.map_errors!({
|
|
50
50
|
'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
|
|
51
51
|
'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
|
|
52
52
|
})
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'irb'
|
|
4
4
|
require 'jsonapi_errors_handler/version'
|
|
5
|
+
require 'jsonapi_errors_handler/configuration'
|
|
5
6
|
require 'jsonapi_errors_handler/errors'
|
|
6
7
|
require 'jsonapi_errors_handler/error_mapper'
|
|
7
8
|
require 'jsonapi_errors_handler/error_serializer'
|
|
@@ -19,22 +20,26 @@ module JsonapiErrorsHandler
|
|
|
19
20
|
end
|
|
20
21
|
|
|
21
22
|
def handle_error(error)
|
|
22
|
-
mapped =
|
|
23
|
-
|
|
24
|
-
mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
|
|
25
|
-
render_error(mapped)
|
|
23
|
+
mapped = ErrorMapper.mapped_error(error)
|
|
24
|
+
mapped ? render_error(mapped) : handle_unexpected_error(error)
|
|
26
25
|
end
|
|
27
26
|
|
|
28
|
-
def
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return error if error.instance_of?(error_klass)
|
|
33
|
-
|
|
34
|
-
Object.const_get(ErrorMapper.mapped_errors[error_klass.to_s]).new
|
|
27
|
+
def handle_unexpected_error(error)
|
|
28
|
+
return raise error unless config.handle_unexpected?
|
|
29
|
+
log_error(error) if respond_to?(:log_error)
|
|
30
|
+
render_error(::JsonapiErrorsHandler::Errors::StandardError.new)
|
|
35
31
|
end
|
|
36
32
|
|
|
37
33
|
def render_error(error)
|
|
38
34
|
render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
|
|
39
35
|
end
|
|
36
|
+
|
|
37
|
+
def self.configure(&block)
|
|
38
|
+
config = Configuration.instance
|
|
39
|
+
config.configure(&block)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.config
|
|
43
|
+
Configuration.instance
|
|
44
|
+
end
|
|
40
45
|
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'singleton'
|
|
4
|
+
|
|
5
|
+
module JsonapiErrorsHandler
|
|
6
|
+
class Configuration
|
|
7
|
+
include Singleton
|
|
8
|
+
|
|
9
|
+
attr_writer :handle_unexpected
|
|
10
|
+
|
|
11
|
+
def configure
|
|
12
|
+
yield(self) if block_given?
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def handle_unexpected?
|
|
16
|
+
@handle_unexpected
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def initialize
|
|
22
|
+
@handle_unexpected = false
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -14,5 +14,20 @@ module JsonapiErrorsHandler
|
|
|
14
14
|
def self.mapped_error?(error_klass)
|
|
15
15
|
mapped_errors.key?(error_klass)
|
|
16
16
|
end
|
|
17
|
+
|
|
18
|
+
def self.mapped_error(error)
|
|
19
|
+
return error if descendant_of_predefined?(error)
|
|
20
|
+
|
|
21
|
+
error_class = error.is_a?(Class) ? error.to_s : error.class.name
|
|
22
|
+
root_class = error_class.split('::').first
|
|
23
|
+
mapped = mapped_errors[error_class] || mapped_errors[root_class]
|
|
24
|
+
return unless mapped
|
|
25
|
+
Object.const_get(mapped).new
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.descendant_of_predefined?(error)
|
|
29
|
+
return false if error.is_a?(Class)
|
|
30
|
+
error.class < JsonapiErrorsHandler::Errors::StandardError
|
|
31
|
+
end
|
|
17
32
|
end
|
|
18
33
|
end
|
|
@@ -5,7 +5,7 @@ require 'jsonapi_errors_handler/keys_stringifier'
|
|
|
5
5
|
module JsonapiErrorsHandler
|
|
6
6
|
module Errors
|
|
7
7
|
class StandardError < ::StandardError
|
|
8
|
-
def initialize(title: nil, detail: nil, status: nil, source: {})
|
|
8
|
+
def initialize(title: nil, detail: nil, status: nil, message: nil, source: {})
|
|
9
9
|
@title = title || 'Something went wrong'
|
|
10
10
|
@detail = detail
|
|
11
11
|
@detail ||= 'We encountered unexpected error, but our developers had been already notified about it'
|
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.1.
|
|
4
|
+
version: 0.1.6
|
|
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-12-
|
|
11
|
+
date: 2019-12-26 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -77,6 +77,7 @@ files:
|
|
|
77
77
|
- bin/setup
|
|
78
78
|
- jsonapi_errors_handler.gemspec
|
|
79
79
|
- lib/jsonapi_errors_handler.rb
|
|
80
|
+
- lib/jsonapi_errors_handler/configuration.rb
|
|
80
81
|
- lib/jsonapi_errors_handler/error_mapper.rb
|
|
81
82
|
- lib/jsonapi_errors_handler/error_serializer.rb
|
|
82
83
|
- lib/jsonapi_errors_handler/errors.rb
|