jsonapi_errors_handler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb11b3f42d8dc4e23da33a7c3768b59f551b0ba5c8f41dc31ae794543751f888
4
- data.tar.gz: 0f3861d1e70dffac65da8acb2c15bf6cd3e2ab056a798ecfb64158df0585d5a7
3
+ metadata.gz: 14dce3e97fbd94737b08f9c1d52b5637f38f81e347589a9cee9f095305dab36a
4
+ data.tar.gz: 95774806d217b6d842edbd8fb40dbf1cf2b391869b838a40df5dbf406ef357d7
5
5
  SHA512:
6
- metadata.gz: d9d1cb4e282c15cdc999df5a6181059b5ddaec11b2494fb5e37bef0ab17f2a968431d4acbf137ec3c676827c5542e920cc1c75b218b71345b739deacf624873a
7
- data.tar.gz: c128b273ce317c637992c3b04b24c5ad9382d04dbc9793184e0197ce2e97bf15889c0f529fd964b970455d72f8eccaa3c00422b445d7566aef34c1794aad132d
6
+ metadata.gz: 27beb749c4762b1348e333fa5553e4f03abeef2f2098493cf8ddadbd6ba3e40f8e8ce3f89bd04b7c28db4f8b68e800017edbac601409cf4f72e32da56aeac303
7
+ data.tar.gz: d432bb2937e6acd84442175d66dc17279e473ab291eb87bc5633740b08669b3e7e0547f9a15a32909942dedc98131b24475b89b4d68c705db3a35560234c37bd
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # JsonapiErrorsHandler
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonapi_errors_handler`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A convienient way to serialize errors in JsonAPI format (https://jsonapi.org)
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,30 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ In your controller:
24
+
25
+ ```
26
+ include JsonapiErrorsHandler
27
+ ```
28
+
29
+ From this point you'll have default html errors being serialized. JsonapiErrorsHandler offers 4 predefined errors:
30
+ - JsonapiErrorsHandler::Errors::Invalid
31
+ - JsonapiErrorsHandler::Errors::Forbidden
32
+ - JsonapiErrorsHandler::Errors::NotFound
33
+ - JsonapiErrorsHandler::Errors::Unauthorized
34
+
35
+ If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
36
+
37
+ ### Custom errors mapping
38
+
39
+ If you want your custom errors being handled by default, just add them to th emapper
40
+
41
+ ```
42
+ ErrorsMapper.map_errors!({
43
+ 'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
44
+ 'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
45
+ })
46
+ ```
26
47
 
27
48
  ## Development
28
49
 
@@ -32,7 +53,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
53
 
33
54
  ## Contributing
34
55
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonapi_errors_handler.
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/driggl/jsonapi_errors_handler.
36
57
 
37
58
  ## License
38
59
 
Binary file
@@ -1,5 +1,44 @@
1
1
  require "jsonapi_errors_handler/version"
2
+ require "jsonapi_errors_handler/error_mapper"
3
+ require "jsonapi_errors_handler/error_serializer"
2
4
 
3
5
  module JsonapiErrorsHandler
4
- # Your code goes here...
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.class_eval do
9
+ ErrorsMapper.map_errors!({
10
+ 'JsonapiErrorsHandler::Errors::Invalid' => 'JsonapiErrorsHandler::Errors::Invalid',
11
+ 'JsonapiErrorsHandler::Errors::Forbidden' => 'JsonapiErrorsHandler::Errors::Forbidden',
12
+ 'JsonapiErrorsHandler::Errors::NotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
13
+ 'JsonapiErrorsHandler::Errors::Unauthorized' => 'JsonapiErrorsHandler::Errors::Unauthorized'
14
+ })
15
+
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
31
+ end
32
+ end
33
+
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
39
+
40
+ def render_error(error)
41
+ render json: ::JsonapiErrorHandler::ErrorSerializer.new(error), status: error.status
42
+ end
43
+ end
5
44
  end
@@ -0,0 +1,15 @@
1
+ module JsonapiErrorsHandler
2
+ class ErrorMapper
3
+ cattr_accessor :mapped_errors
4
+
5
+ def self.map_errors!(errors_hash={})
6
+ @@mapped_errors ||= {}
7
+ @@mapped_errors.merge!(errors_hash)
8
+ end
9
+
10
+ def self.mapped_error?(error_klass)
11
+ mapped_errors.values.include?(error_klass)
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,26 @@
1
+ module JsonapiErrorsHandler
2
+ class ErrorSerializer
3
+ def initialize(error)
4
+ @error = error
5
+ end
6
+
7
+ def to_h
8
+ serializable_hash
9
+ end
10
+
11
+ def to_json(payload)
12
+ to_h.to_json
13
+ end
14
+
15
+ private
16
+
17
+ def serializable_hash
18
+ {
19
+ errors: Array.wrap(error.serializable_hash)
20
+ }
21
+ end
22
+
23
+ attr_reader :error
24
+ end
25
+ end
26
+
@@ -0,0 +1,11 @@
1
+ module JsonapiErrorsHandler
2
+ module Errors
3
+
4
+ end
5
+ end
6
+
7
+ require_dependency 'jsonapi_errors_handler/errors/standard_error'
8
+ require_dependency 'jsonapi_errors_handler/errors/forbidden'
9
+ require_dependency 'jsonapi_errors_handler/errors/invalid'
10
+ require_dependency 'jsonapi_errors_handler/errors/not_found'
11
+ require_dependency 'jsonapi_errors_handler/errors/unauthorized'
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiErrorsHandler
4
+ module Errors
5
+ class Forbidden < ::JsonapiErrorsHandler::Errors::StandardError
6
+ def initialize(message: nil)
7
+ super(
8
+ title: "Forbidden request",
9
+ status: 403,
10
+ detail: message || "You have no rights to access this resource",
11
+ source: { pointer: "/request/headers/authorization" }
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiErrorsHandler
4
+ module Errors
5
+ class Invalid < ::JsonapiErrorsHandler::Errors::StandardError
6
+ def initialize(errors: {})
7
+ @errors = errors
8
+ @status = 422
9
+ @title = "Invalid request"
10
+ end
11
+
12
+ def serializable_hash
13
+ errors.reduce([]) do |r, (att, msg)|
14
+ r << {
15
+ status: status,
16
+ title: title,
17
+ detail: msg,
18
+ source: { pointer: "/data/attributes/#{att}" }
19
+ }
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :errors
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiErrorsHandler
4
+ module Errors
5
+ class NotFound < ::JsonapiErrorsHandler::Errors::StandardError
6
+ def initialize(message: nil)
7
+ super(
8
+ title: "Record not Found",
9
+ status: 404,
10
+ detail: "We could not find the object you were looking for.",
11
+ source: { pointer: "/request/url/:id" }
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiErrorsHandler
4
+ module Errors
5
+ class StandardError < ::StandardError
6
+ def initialize(title: nil, detail: nil, status: nil, source: {})
7
+ @title = title || "Something went wrong"
8
+ @detail = detail || "We encountered unexpected error, but our developers had been already notified about it"
9
+ @status = status || 500
10
+ @source = source.deep_stringify_keys
11
+ end
12
+
13
+ def to_h
14
+ {
15
+ status: status,
16
+ title: title,
17
+ detail: detail,
18
+ source: source
19
+ }
20
+ end
21
+
22
+ def serializable_hash
23
+ to_h
24
+ end
25
+
26
+ def to_s
27
+ to_h.to_s
28
+ end
29
+
30
+ attr_reader :title, :detail, :status, :source
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonapiErrorsHandler
4
+ module Errors
5
+ class Unauthorized < ::JsonapiErrorsHandler::Errors::StandardError
6
+ def initialize(message: nil)
7
+ super(
8
+ title: "Unauthorized",
9
+ status: 401,
10
+ detail: message || "You need to login to authorize this request.",
11
+ source: { pointer: "/request/headers/authorization" }
12
+ )
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module JsonapiErrorsHandler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
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.1
4
+ version: 0.0.2
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-25 00:00:00.000000000 Z
11
+ date: 2019-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,8 +71,17 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - jsonapi_errors_handler-0.0.0.gem
74
+ - jsonapi_errors_handler-0.0.1.gem
74
75
  - jsonapi_errors_handler.gemspec
75
76
  - lib/jsonapi_errors_handler.rb
77
+ - lib/jsonapi_errors_handler/error_mapper.rb
78
+ - lib/jsonapi_errors_handler/error_searializer.rb
79
+ - lib/jsonapi_errors_handler/errors.rb
80
+ - lib/jsonapi_errors_handler/errors/forbidden.rb
81
+ - lib/jsonapi_errors_handler/errors/invalid.rb
82
+ - lib/jsonapi_errors_handler/errors/not_found.rb
83
+ - lib/jsonapi_errors_handler/errors/standard_error.rb
84
+ - lib/jsonapi_errors_handler/errors/unauthorized.rb
76
85
  - lib/jsonapi_errors_handler/version.rb
77
86
  homepage: https://github.com/driggl/jsonapi_error_handler
78
87
  licenses: