jsonapi_errors_handler 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.ruby-version +1 -0
- data/CONTRIBUTING.md +21 -0
- data/Gemfile +4 -2
- data/README.md +77 -23
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/jsonapi_errors_handler.gemspec +24 -19
- data/lib/jsonapi_errors_handler.rb +24 -27
- data/lib/jsonapi_errors_handler/error_mapper.rb +4 -3
- data/lib/jsonapi_errors_handler/error_serializer.rb +6 -3
- data/lib/jsonapi_errors_handler/errors.rb +2 -1
- data/lib/jsonapi_errors_handler/errors/forbidden.rb +3 -3
- data/lib/jsonapi_errors_handler/errors/invalid.rb +1 -1
- data/lib/jsonapi_errors_handler/errors/not_found.rb +3 -4
- data/lib/jsonapi_errors_handler/errors/standard_error.rb +6 -3
- data/lib/jsonapi_errors_handler/errors/unauthorized.rb +3 -3
- data/lib/jsonapi_errors_handler/keys_stringifier.rb +12 -0
- data/lib/jsonapi_errors_handler/version.rb +3 -1
- data/package-lock.json +2723 -0
- data/package.json +8 -0
- metadata +10 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a2f52ab77c66d7116cda41a5cb4e087a94621967689382b7de9466fa270c0ac
|
4
|
+
data.tar.gz: 9efb9e2a227bc6efe491573ca8b675cf53c770b8b437ed5e9523dffbc60e9628
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f0e2fedb96b9cef8d8f606bf3bd76cb0e40909ce6b82d707fa95ad853188cb196e814ad97af089c9f3780831c148edd971da0df3b11f76b26776fc8c7359098
|
7
|
+
data.tar.gz: 6014da1d66dbb431b944b44f74d249510c7236db2802ee728d21ddb337b259a41d7a96bbcf0a0893e98bf009094ba78c5d1061d8d1485847e74a6d0da2e8c0cb
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.1
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
## Contributing
|
2
|
+
|
3
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
|
4
|
+
|
5
|
+
**How to contribute:**
|
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
|
17
|
+
|
18
|
+
**Coding Guidelines**
|
19
|
+
|
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.
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in jsonapi_errors_handler.gemspec
|
6
8
|
gemspec
|
data/README.md
CHANGED
@@ -1,16 +1,17 @@
|
|
1
|
-
|
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
|
+
[![Gem Version](https://badge.fury.io/rb/jsonapi_errors_handler.svg)](https://badge.fury.io/rb/jsonapi_errors_handler)
|
2
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)
|
3
4
|
|
4
5
|
# JsonapiErrorsHandler
|
5
6
|
|
6
|
-
A convienient way to serialize errors in
|
7
|
+
A convienient way to serialize errors in [Jsonapi standard](https://jsonapi.org)
|
7
8
|
|
8
9
|
## Installation
|
9
10
|
|
10
11
|
Add this line to your application's Gemfile:
|
11
12
|
|
12
13
|
```ruby
|
13
|
-
gem 'jsonapi_errors_handler'
|
14
|
+
gem 'jsonapi_errors_handler'
|
14
15
|
```
|
15
16
|
|
16
17
|
And then execute:
|
@@ -25,16 +26,17 @@ Or install it yourself as:
|
|
25
26
|
|
26
27
|
In your controller:
|
27
28
|
|
28
|
-
```
|
29
|
-
include JsonapiErrorsHandler
|
30
|
-
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
|
29
|
+
```ruby
|
30
|
+
include JsonapiErrorsHandler
|
31
|
+
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
|
31
32
|
```
|
32
33
|
|
33
34
|
From this point you'll have default html errors being serialized. JsonapiErrorsHandler offers 4 predefined errors:
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
|
36
|
+
* [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
|
37
|
+
* [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
|
38
|
+
* [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)
|
38
40
|
|
39
41
|
If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
|
40
42
|
|
@@ -42,15 +44,66 @@ If you rise any of errors above in any place of your application, client gets th
|
|
42
44
|
|
43
45
|
If you want your custom errors being handled by default, just add them to the mapper
|
44
46
|
|
47
|
+
```ruby
|
48
|
+
include JsonapiErrorsHandler
|
49
|
+
ErrorsMapper.map_errors!({
|
50
|
+
'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
|
51
|
+
'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
|
52
|
+
})
|
53
|
+
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
|
54
|
+
```
|
55
|
+
|
56
|
+
###Custom error logging
|
57
|
+
|
58
|
+
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
|
+
way.
|
60
|
+
|
61
|
+
However, you'd probably like to have a way to log the risen error on your own to send notifications to developers that
|
62
|
+
something unexpected happened.
|
63
|
+
|
64
|
+
To do so, just implement the `log_error` method in your controller, that accepts the risen error as an argument.
|
65
|
+
|
45
66
|
```
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
|
50
|
-
})
|
51
|
-
rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
|
67
|
+
def log_error(error)
|
68
|
+
#do the fancy logging here
|
69
|
+
end
|
52
70
|
```
|
53
71
|
|
72
|
+
### Custom error responses
|
73
|
+
|
74
|
+
By default, we deliver hardcoded responses. You can check out the defined error classes for details
|
75
|
+
|
76
|
+
* [JsonapiErrorsHandler::Errors::Invalid](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/invalid.rb)
|
77
|
+
* [JsonapiErrorsHandler::Errors::Forbidden](https://github.com/driggl/jsonapi_errors_handler/blob/master/lib/jsonapi_errors_handler/errors/forbidden.rb)
|
78
|
+
* [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)
|
80
|
+
|
81
|
+
If you want to have custom error responses being delivered, just create your own `Api::Errors` that inherits from `JsonapiErrorsHandler::StandardError`
|
82
|
+
|
83
|
+
### Localization example
|
84
|
+
|
85
|
+
If you want to localize your responses, just create a class:
|
86
|
+
|
87
|
+
```
|
88
|
+
module Api::Errors
|
89
|
+
class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
|
90
|
+
def initialize(*)
|
91
|
+
super(
|
92
|
+
title: I18n.t('api.errors.forbidden.title'),
|
93
|
+
status: 403,
|
94
|
+
detail: I18n.t('api.errors.forbidden.detail'),
|
95
|
+
source: { pointer: '/request/headers/authorization' }
|
96
|
+
)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
## Guides & tutorials
|
103
|
+
|
104
|
+
* [Handling Exceptions in Rails Applications](https://driggl.com/blog/a/handling-exceptions-in-rails-applications) - Gem's concept explained in details
|
105
|
+
* [JsonApi Errors Handler Guide](https://driggl.com/blog/a/json-api-errors-handler)
|
106
|
+
|
54
107
|
## Development
|
55
108
|
|
56
109
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -59,16 +112,17 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
59
112
|
|
60
113
|
## Contributing
|
61
114
|
|
62
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
|
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).
|
63
116
|
|
64
117
|
**How to contribute:**
|
65
118
|
|
66
|
-
1.
|
67
|
-
2.
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
119
|
+
1. Fork repository
|
120
|
+
2. Install [Rubocop](https://github.com/rubocop-hq/rubocop) - make sure you run it before commiting changes
|
121
|
+
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
|
125
|
+
4. Create Pull Request
|
72
126
|
|
73
127
|
## License
|
74
128
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'jsonapi_errors_handler'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "jsonapi_errors_handler"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
@@ -1,41 +1,46 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
2
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require
|
5
|
+
require 'jsonapi_errors_handler/version'
|
4
6
|
|
5
7
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
8
|
+
spec.name = 'jsonapi_errors_handler'
|
7
9
|
spec.version = JsonapiErrorsHandler::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
+
spec.authors = ['Sebastian Wilgosz']
|
11
|
+
spec.email = ['sebastian@driggl.com']
|
10
12
|
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
|
14
|
-
|
13
|
+
spec.summary = 'A JSON API error handler for ruby applications.'
|
14
|
+
spec.description = <<~STRING
|
15
|
+
This gem is a convienient wrapper for your application errors.
|
16
|
+
It allows you to map any error to a nicely formatted standard HTTP error response.
|
17
|
+
STRING
|
18
|
+
spec.homepage = 'https://github.com/driggl/jsonapi_errors_handler'
|
19
|
+
spec.license = 'MIT'
|
15
20
|
|
16
21
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
22
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
23
|
if spec.respond_to?(:metadata)
|
19
24
|
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
25
|
|
21
|
-
spec.metadata[
|
22
|
-
spec.metadata[
|
26
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
27
|
+
spec.metadata['source_code_uri'] = 'https://github.com/driggl/jsonapi_errors_handler'
|
23
28
|
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
24
29
|
else
|
25
|
-
raise
|
26
|
-
|
30
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
31
|
+
'public gem pushes.'
|
27
32
|
end
|
28
33
|
|
29
34
|
# Specify which files should be added to the gem when it is released.
|
30
35
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
-
spec.files
|
36
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
37
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
38
|
end
|
34
|
-
spec.bindir =
|
39
|
+
spec.bindir = 'exe'
|
35
40
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
-
spec.require_paths = [
|
41
|
+
spec.require_paths = ['lib']
|
37
42
|
|
38
|
-
spec.add_development_dependency
|
39
|
-
spec.add_development_dependency
|
40
|
-
spec.add_development_dependency
|
43
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
44
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
45
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
41
46
|
end
|
@@ -1,41 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'irb'
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
4
|
+
require 'jsonapi_errors_handler/version'
|
5
|
+
require 'jsonapi_errors_handler/errors'
|
6
|
+
require 'jsonapi_errors_handler/error_mapper'
|
7
|
+
require 'jsonapi_errors_handler/error_serializer'
|
6
8
|
|
7
9
|
module JsonapiErrorsHandler
|
8
10
|
def self.included(base)
|
9
11
|
base.class_eval do
|
10
|
-
ErrorMapper.map_errors!(
|
12
|
+
ErrorMapper.map_errors!(
|
11
13
|
'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
|
12
14
|
'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
|
13
15
|
'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
|
14
16
|
'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
|
15
|
-
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
if mapped
|
24
|
-
render_error(mapped)
|
25
|
-
else
|
26
|
-
raise e
|
27
|
-
end
|
28
|
-
end
|
21
|
+
def handle_error(error)
|
22
|
+
mapped = map_error(error)
|
23
|
+
log_error(error) if respond_to?(:log_error) && !mapped
|
24
|
+
mapped ||= ::JsonapiErrorsHandler::Errors::StandardError.new
|
25
|
+
render_error(mapped)
|
26
|
+
end
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
ErrorMapper.mapped_errors[error_klass]&.constantize&.new
|
34
|
-
end
|
28
|
+
def map_error(error)
|
29
|
+
error_klass = error.class.name
|
30
|
+
return nil unless ErrorMapper.mapped_error?(error_klass)
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
Object.const_get(ErrorMapper.mapped_errors[error_klass]).new
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_error(error)
|
36
|
+
render json: ::JsonapiErrorsHandler::ErrorSerializer.new(error), status: error.status
|
40
37
|
end
|
41
38
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module JsonapiErrorsHandler
|
2
4
|
class ErrorMapper
|
3
5
|
@@mapped_errors = {}
|
@@ -5,13 +7,12 @@ module JsonapiErrorsHandler
|
|
5
7
|
@@mapped_errors
|
6
8
|
end
|
7
9
|
|
8
|
-
def self.map_errors!(errors_hash={})
|
10
|
+
def self.map_errors!(errors_hash = {})
|
9
11
|
@@mapped_errors.merge!(errors_hash)
|
10
12
|
end
|
11
13
|
|
12
14
|
def self.mapped_error?(error_klass)
|
13
|
-
mapped_errors.
|
15
|
+
mapped_errors.value?(error_klass)
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
17
|
-
|
@@ -1,3 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
1
5
|
module JsonapiErrorsHandler
|
2
6
|
class ErrorSerializer
|
3
7
|
def initialize(error)
|
@@ -8,7 +12,7 @@ module JsonapiErrorsHandler
|
|
8
12
|
serializable_hash
|
9
13
|
end
|
10
14
|
|
11
|
-
def to_json(_payload=nil)
|
15
|
+
def to_json(_payload = nil)
|
12
16
|
to_h.to_json
|
13
17
|
end
|
14
18
|
|
@@ -16,11 +20,10 @@ module JsonapiErrorsHandler
|
|
16
20
|
|
17
21
|
def serializable_hash
|
18
22
|
{
|
19
|
-
errors:
|
23
|
+
errors: [error.serializable_hash]
|
20
24
|
}
|
21
25
|
end
|
22
26
|
|
23
27
|
attr_reader :error
|
24
28
|
end
|
25
29
|
end
|
26
|
-
|
@@ -5,10 +5,10 @@ module JsonapiErrorsHandler
|
|
5
5
|
class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
|
6
6
|
def initialize(message: nil)
|
7
7
|
super(
|
8
|
-
title:
|
8
|
+
title: 'Forbidden request',
|
9
9
|
status: 403,
|
10
|
-
detail: message ||
|
11
|
-
source: { pointer:
|
10
|
+
detail: message || 'You have no rights to access this resource',
|
11
|
+
source: { pointer: '/request/headers/authorization' }
|
12
12
|
)
|
13
13
|
end
|
14
14
|
end
|