jsonapi_errors_handler 0.1.6 → 0.1.7

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: ba2a86c74563398a7436e21ceea674a26a9672bbfba6c8fadc125df8eed70615
4
- data.tar.gz: f56a1281368f6ef3ab4c3a2a38afd1c80affd3997c32acca0f5cfe0784b4ba95
3
+ metadata.gz: df8269ff040273b0368d86c043bf1cd50fb29503be3d4b5207ffba0aa89d27b8
4
+ data.tar.gz: 7d85f4979969cf3e914145633c11abc0c6d3ff325671bbe788c614ebb5292d59
5
5
  SHA512:
6
- metadata.gz: 3661da30d706ed7cdc762136424326c0bca8d749afee213f80479e420d7f639bc1be1ef39954fced7ebf01c2a7a25aa8358dfe9ef5cc38ffb63f2f2984e4da86
7
- data.tar.gz: 62f72aa1e1ea8df5b8b68733a14cd3df02705fa47bd4b5c2f1dc5bdc66ef9f3ceb376473608d3927a9eb0669f569aa43e290e7cdc3011635eee35f038510933c
6
+ metadata.gz: 0d18a2f65e6a93525ec2c45a04714efd01fc8894a87f4d54ffce2652e73c6320ae5ed5447db4499c085a4e335a9162186c1a757d8a6ef6fb8c96bf0c7e6fc625
7
+ data.tar.gz: 2a3491ecaa2b1c1fcffdbc4d6bcaa430fb80c77c6daa9af55121b4d2ff5532b14781fb07c9a174aeec0e17599700fd50f3629988f99501c1ff0d0f23773186dd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi_errors_handler (0.1.5)
4
+ jsonapi_errors_handler (0.1.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -40,6 +40,18 @@ From this point you'll have default html errors being serialized. JsonapiErrorsH
40
40
 
41
41
  If you rise any of errors above in any place of your application, client gets the nicely formatted error message instead of 500
42
42
 
43
+ ### Handling unexpected errors
44
+
45
+ If you want to handle all the errors in your API application to deliver nicely formatted JSON response about 500 instead crashing the server, add this when your application loads:
46
+
47
+ ```ruby
48
+ require 'jsonapi_errors_handler'
49
+
50
+ JsonapiErrorsHandler.configure do |config|
51
+ config.handle_unexpected = true
52
+ end
53
+ ```
54
+
43
55
  ### Custom errors mapping
44
56
 
45
57
  If you want your custom errors being handled by default, just add them to the mapper
@@ -47,13 +59,28 @@ If you want your custom errors being handled by default, just add them to the ma
47
59
  ```ruby
48
60
  include JsonapiErrorsHandler
49
61
  ErrorMapper.map_errors!({
50
- 'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound',
51
- 'ActiveRecord::RecordInvalid' => 'JsonapiErrorsHandler::Errors::Invalid',
62
+ 'ActiveRecord::RecordNotFound' => 'JsonapiErrorsHandler::Errors::NotFound'
52
63
  })
53
64
  rescue_from ::StandardError, with: lambda { |e| handle_error(e) }
54
65
  ```
55
66
 
56
- ###Custom error logging
67
+ ### Handling rails-specific validation errors
68
+
69
+ To handle validation errors from ActiveRecord or ActiveModel, you need to write custom
70
+ error handler:
71
+
72
+ ```ruby
73
+ rescue_from ActiveRecord::RecordInvalid, with: lambda { |e| handle_validation_error(e) }
74
+ rescue_from ActiveModel::ValidationError, with: lambda { |e| handle_validation_error(e) }
75
+
76
+ def handle_validation_error(error)
77
+ error_model = error.try(:model) || error.try(:record)
78
+ mapped = JsonapiErrorsHandler::Errors::Invalid.new(errors: error_model.errors)
79
+ render_error(mapped)
80
+ end
81
+ ```
82
+
83
+ ### Custom error logging
57
84
 
58
85
  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
86
  way.
@@ -39,7 +39,7 @@ module JsonapiErrorsHandler
39
39
  config.configure(&block)
40
40
  end
41
41
 
42
- def self.config
42
+ def config
43
43
  Configuration.instance
44
44
  end
45
45
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonapiErrorsHandler
4
- VERSION = '0.1.6'
4
+ VERSION = '0.1.7'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_errors_handler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Wilgosz