openstax_rescue_from 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +32 -47
- data/lib/openstax/rescue_from/default_exceptions.rb +4 -0
- data/lib/openstax/rescue_from/logger.rb +1 -1
- data/lib/openstax/rescue_from/version.rb +1 -1
- data/lib/openstax/rescue_from.rb +7 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NmZjZTU4YTYyNGM5MDQyYmQwYTJiMzIxZGM3MDRkNmM1M2RjZmM4ZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTdjZDk5YTAwMWY5YzU2NThjNjBiOWQ1YmM1ZWUwZDdjNzIxYjJkOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2RmMGEwOWJkMjkyZjk4OGFmOGYxMGUyNDM5MjcwMDYwMWMyNTQyMzBkYWU2
|
10
|
+
YzcyZDRlYzJkNTNjNmI2ODFhZDRhOTQ1NjQ0N2MyZmJiOGM3ZWEzNTI0N2U2
|
11
|
+
MzIxMDNmMjczNjkxY2RkODY5YWI2ZTg5ZjZiNDQ0ODYzMTZkMGE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDUyNGM4ZTZkMGZjYjFkNGM4NDNmNGVhMWZmYmVhMWExZjc3NmJlNjhlY2Q5
|
14
|
+
OWNjOTBiZjUwZmE4YTY5MDQ4NjdmNjgzNDVhNTBhNzEzOGRmMmQ4NmMzZjYx
|
15
|
+
NmJjMzBjZDhkOWRiY2E5YzEyMTlmMjMwZmRlZWM5MDdlYjhkYTI=
|
data/README.md
CHANGED
@@ -56,6 +56,36 @@ class ApplicationController < ActionController::Base
|
|
56
56
|
end
|
57
57
|
```
|
58
58
|
|
59
|
+
## Registering Exceptions
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# Use OpenStax::RescueFrom.register_exception(exception_constant_or_string, options = {})
|
63
|
+
# to register new exceptions or override the options of existing ones
|
64
|
+
|
65
|
+
OpenStax::RescueFrom.register_exception(SecurityTransgression, status: 403,
|
66
|
+
extras: -> (exception) {
|
67
|
+
{ headers: exception.response.headers }
|
68
|
+
})
|
69
|
+
|
70
|
+
OpenStax::RescueFrom.register_exception('ActiveRecord::RecordNotFound', notify: false,
|
71
|
+
status: 404)
|
72
|
+
|
73
|
+
# Use OpenStax::RescueFrom.register_unrecognized_exception(exception_constant_or_string, options = {})
|
74
|
+
# to register ONLY unrecognized exceptions, to avoid accidental overwriting of options
|
75
|
+
|
76
|
+
OpenStax::RescueFrom.register_unrecognized_exception(SecurityTransgression)
|
77
|
+
|
78
|
+
# when used with example above, the above example's options will prevail
|
79
|
+
|
80
|
+
# Default options:
|
81
|
+
#
|
82
|
+
# { notify: true,
|
83
|
+
# status: :internal_server_error,
|
84
|
+
# extras: ->(exception) { {} } }
|
85
|
+
```
|
86
|
+
|
87
|
+
**Note:** If you want to define `extras`, you **must** use a function that accepts `exception` as its argument
|
88
|
+
|
59
89
|
## Configuration
|
60
90
|
|
61
91
|
This configuration, which is placed in `./config/initializers/rescue_from.rb` by the install generator, shows the defaults:
|
@@ -81,52 +111,6 @@ OpenStax::RescueFrom.configure do |config|
|
|
81
111
|
end
|
82
112
|
```
|
83
113
|
|
84
|
-
## Registering exceptions
|
85
|
-
|
86
|
-
In `./config/initializers/rescue_from.rb` it is recommended you register your exceptions
|
87
|
-
|
88
|
-
Note that any unregistered exceptions rescued during run-time will be registered with the default options. See below.
|
89
|
-
|
90
|
-
```ruby
|
91
|
-
require 'openstax_rescue_from'
|
92
|
-
|
93
|
-
OpenStax::RescueFrom.configure do |c|
|
94
|
-
# c.app_name ...
|
95
|
-
end
|
96
|
-
|
97
|
-
# OpenStax::RescueFrom#register_exception default options:
|
98
|
-
#
|
99
|
-
# { notify: true,
|
100
|
-
# status: :internal_server_error,
|
101
|
-
# extras: ->(exception) { {} } }
|
102
|
-
#
|
103
|
-
# Any unregistered exceptions rescued during run-time
|
104
|
-
# will be registered during rescue with the options above
|
105
|
-
|
106
|
-
OpenStax::RescueFrom.register_exception('SecurityTransgression',
|
107
|
-
notify: false,
|
108
|
-
status: :forbidden)
|
109
|
-
|
110
|
-
OpenStax::RescueFrom.register_exception(ActiveRecord::NotFound,
|
111
|
-
notify: false,
|
112
|
-
status: :not_found)
|
113
|
-
|
114
|
-
OpenStax::RescueFrom.register_exception('OAuth2::Error',
|
115
|
-
extras: ->(exception) {
|
116
|
-
{ headers: exception.response.headers,
|
117
|
-
status: exception.response.status,
|
118
|
-
body: exception.response.body }
|
119
|
-
})
|
120
|
-
|
121
|
-
OpenStax::RescueFrom.translate_status_codes({
|
122
|
-
forbidden: 'You are not allowed to access this.',
|
123
|
-
not_found: 'We couldn't find what you asked for.',
|
124
|
-
})
|
125
|
-
#
|
126
|
-
# Default:
|
127
|
-
# internal_server_error: "Sorry, #{OpenStax::RescueFrom.configuration.app_name} had some unexpected trouble with your request."
|
128
|
-
```
|
129
|
-
|
130
114
|
## Controller hook
|
131
115
|
```ruby
|
132
116
|
#
|
@@ -139,7 +123,7 @@ OpenStax::RescueFrom.translate_status_codes({
|
|
139
123
|
# Params:
|
140
124
|
# exception_proxy - an OpenStax::RescueFrom::ExceptionProxy wrapper around
|
141
125
|
# the exception
|
142
|
-
# did_notify - true
|
126
|
+
# did_notify - true if the exception was sent out to notification channels
|
143
127
|
# such as email or the log file
|
144
128
|
#
|
145
129
|
|
@@ -147,6 +131,7 @@ def openstax_exception_rescued(exception_proxy, did_notify)
|
|
147
131
|
@message = exception_proxy.friendly_message
|
148
132
|
@status = exception_proxy.status
|
149
133
|
@error_id = exception_proxy.error_id
|
134
|
+
@did_notify = did_notify
|
150
135
|
|
151
136
|
respond_to do |f|
|
152
137
|
f.html { render template: openstax_rescue_config.html_error_template_path,
|
@@ -40,6 +40,10 @@ module OpenStax
|
|
40
40
|
RescueFrom.register_exception('Apipie::ParamMissing',
|
41
41
|
notify: false,
|
42
42
|
status: :unprocessable_entity)
|
43
|
+
|
44
|
+
RescueFrom.register_exception(ActionController::UnknownHttpMethod,
|
45
|
+
notify: false,
|
46
|
+
status: :bad_request)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
end
|
data/lib/openstax/rescue_from.rb
CHANGED
@@ -7,11 +7,8 @@ module OpenStax
|
|
7
7
|
module RescueFrom
|
8
8
|
class << self
|
9
9
|
def perform_rescue(exception, listener = MuteListener.new)
|
10
|
-
unless registered_exceptions.keys.include?(exception.class.name)
|
11
|
-
register_exception(exception.class)
|
12
|
-
end
|
13
|
-
|
14
10
|
proxy = ExceptionProxy.new(exception)
|
11
|
+
register_unrecognized_exception(proxy.name)
|
15
12
|
log_system_error(proxy)
|
16
13
|
send_notifying_exceptions(proxy, listener)
|
17
14
|
finish_exception_rescue(proxy, listener)
|
@@ -23,6 +20,12 @@ module OpenStax
|
|
23
20
|
@@registered_exceptions[name] = ExceptionOptions.new(options)
|
24
21
|
end
|
25
22
|
|
23
|
+
def register_unrecognized_exception(exception_class, options = {})
|
24
|
+
unless registered_exceptions.keys.include?(exception_class)
|
25
|
+
register_exception(exception_class, options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
26
29
|
def translate_status_codes(map = {})
|
27
30
|
map.each do |k, v|
|
28
31
|
friendly_status_messages[k] = v
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openstax_rescue_from
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JP Slavinsky
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-10-
|
12
|
+
date: 2015-10-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|