jsonapi_errors_handler 0.1.6 → 0.1.7
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 +30 -3
- data/lib/jsonapi_errors_handler.rb +1 -1
- data/lib/jsonapi_errors_handler/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df8269ff040273b0368d86c043bf1cd50fb29503be3d4b5207ffba0aa89d27b8
|
4
|
+
data.tar.gz: 7d85f4979969cf3e914145633c11abc0c6d3ff325671bbe788c614ebb5292d59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d18a2f65e6a93525ec2c45a04714efd01fc8894a87f4d54ffce2652e73c6320ae5ed5447db4499c085a4e335a9162186c1a757d8a6ef6fb8c96bf0c7e6fc625
|
7
|
+
data.tar.gz: 2a3491ecaa2b1c1fcffdbc4d6bcaa430fb80c77c6daa9af55121b4d2ff5532b14781fb07c9a174aeec0e17599700fd50f3629988f99501c1ff0d0f23773186dd
|
data/Gemfile.lock
CHANGED
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
|
-
###
|
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.
|