roda 3.108.0 → 3.110.0
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/lib/roda/cache.rb +0 -2
- data/lib/roda/opts.rb +22 -0
- data/lib/roda/plugins/Integer_matcher_max.rb +1 -1
- data/lib/roda/plugins/_add_script_name.rb +14 -0
- data/lib/roda/plugins/_json.rb +22 -0
- data/lib/roda/plugins/_symbol_class_matchers.rb +8 -10
- data/lib/roda/plugins/additional_render_engines.rb +5 -1
- data/lib/roda/plugins/additional_view_directories.rb +7 -3
- data/lib/roda/plugins/assets.rb +179 -151
- data/lib/roda/plugins/assets_preloading.rb +26 -10
- data/lib/roda/plugins/autoload_hash_branches.rb +9 -3
- data/lib/roda/plugins/autoload_named_routes.rb +9 -3
- data/lib/roda/plugins/backtracking_array.rb +2 -2
- data/lib/roda/plugins/branch_locals.rb +4 -4
- data/lib/roda/plugins/break.rb +2 -2
- data/lib/roda/plugins/caching.rb +27 -17
- data/lib/roda/plugins/capture_erb.rb +19 -12
- data/lib/roda/plugins/chunked.rb +42 -34
- data/lib/roda/plugins/class_level_routing.rb +10 -5
- data/lib/roda/plugins/class_matchers.rb +14 -7
- data/lib/roda/plugins/common_logger.rb +27 -5
- data/lib/roda/plugins/conditional_sessions.rb +4 -4
- data/lib/roda/plugins/content_for.rb +12 -7
- data/lib/roda/plugins/content_security_policy.rb +17 -14
- data/lib/roda/plugins/cookie_flags.rb +17 -7
- data/lib/roda/plugins/cookies.rb +12 -6
- data/lib/roda/plugins/csrf.rb +2 -1
- data/lib/roda/plugins/custom_block_results.rb +11 -10
- data/lib/roda/plugins/custom_matchers.rb +4 -2
- data/lib/roda/plugins/default_headers.rb +4 -7
- data/lib/roda/plugins/default_status.rb +1 -0
- data/lib/roda/plugins/delay_build.rb +1 -1
- data/lib/roda/plugins/delegate.rb +7 -6
- data/lib/roda/plugins/direct_call.rb +5 -1
- data/lib/roda/plugins/disallow_file_uploads.rb +2 -3
- data/lib/roda/plugins/drop_body.rb +3 -6
- data/lib/roda/plugins/each_part.rb +23 -29
- data/lib/roda/plugins/early_hints.rb +4 -4
- data/lib/roda/plugins/empty_root.rb +5 -5
- data/lib/roda/plugins/environments.rb +11 -15
- data/lib/roda/plugins/erb_h.rb +4 -4
- data/lib/roda/plugins/error_email.rb +23 -20
- data/lib/roda/plugins/error_handler.rb +28 -34
- data/lib/roda/plugins/error_mail.rb +31 -16
- data/lib/roda/plugins/exception_page.rb +41 -35
- data/lib/roda/plugins/filter_common_logger.rb +6 -4
- data/lib/roda/plugins/flash.rb +16 -13
- data/lib/roda/plugins/freeze_request_response.rb +24 -0
- data/lib/roda/plugins/hash_branch_view_subdir.rb +6 -4
- data/lib/roda/plugins/hash_branches.rb +7 -5
- data/lib/roda/plugins/hash_paths.rb +7 -5
- data/lib/roda/plugins/hash_public.rb +11 -7
- data/lib/roda/plugins/hash_public_cache.rb +9 -7
- data/lib/roda/plugins/hash_routes.rb +6 -4
- data/lib/roda/plugins/heartbeat.rb +6 -2
- data/lib/roda/plugins/hmac_paths.rb +9 -3
- data/lib/roda/plugins/hooks.rb +13 -10
- data/lib/roda/plugins/host_authorization.rb +7 -2
- data/lib/roda/plugins/host_routing.rb +7 -2
- data/lib/roda/plugins/invalid_request_body.rb +20 -4
- data/lib/roda/plugins/ip_from_header.rb +5 -1
- data/lib/roda/plugins/json.rb +10 -8
- data/lib/roda/plugins/json_parser.rb +26 -12
- data/lib/roda/plugins/mail_processor.rb +9 -6
- data/lib/roda/plugins/mailer.rb +5 -3
- data/lib/roda/plugins/match_affix.rb +6 -1
- data/lib/roda/plugins/match_hook_args.rb +7 -5
- data/lib/roda/plugins/middleware.rb +13 -7
- data/lib/roda/plugins/module_include.rb +1 -1
- data/lib/roda/plugins/multi_public.rb +6 -2
- data/lib/roda/plugins/multi_route.rb +1 -1
- data/lib/roda/plugins/multi_run.rb +13 -17
- data/lib/roda/plugins/named_routes.rb +8 -6
- data/lib/roda/plugins/named_templates.rb +5 -3
- data/lib/roda/plugins/path.rb +16 -14
- data/lib/roda/plugins/path_rewriter.rb +8 -5
- data/lib/roda/plugins/permissions_policy.rb +21 -19
- data/lib/roda/plugins/public.rb +22 -10
- data/lib/roda/plugins/recheck_precompiled_assets.rb +30 -8
- data/lib/roda/plugins/redirect_http_to_https.rb +6 -3
- data/lib/roda/plugins/render.rb +37 -49
- data/lib/roda/plugins/render_coverage.rb +6 -3
- data/lib/roda/plugins/render_locals.rb +13 -7
- data/lib/roda/plugins/request_aref.rb +1 -1
- data/lib/roda/plugins/response_content_type.rb +7 -3
- data/lib/roda/plugins/route_csrf.rb +7 -2
- data/lib/roda/plugins/run_append_slash.rb +5 -1
- data/lib/roda/plugins/sec_fetch_site_csrf.rb +6 -2
- data/lib/roda/plugins/sessions.rb +164 -139
- data/lib/roda/plugins/shape_friendly.rb +3 -1
- data/lib/roda/plugins/sinatra_helpers.rb +7 -3
- data/lib/roda/plugins/status_handler.rb +5 -3
- data/lib/roda/plugins/strip_path_prefix.rb +4 -2
- data/lib/roda/plugins/symbol_matchers.rb +11 -6
- data/lib/roda/plugins/symbol_views.rb +1 -1
- data/lib/roda/plugins/timestamp_public.rb +8 -4
- data/lib/roda/plugins/type_routing.rb +9 -5
- data/lib/roda/plugins/typecast_params.rb +2 -2
- data/lib/roda/plugins/typecast_params_sized_integers.rb +1 -1
- data/lib/roda/plugins/unescape_path.rb +22 -4
- data/lib/roda/plugins/url_escape.rb +2 -14
- data/lib/roda/plugins.rb +34 -0
- data/lib/roda/request.rb +8 -7
- data/lib/roda/response.rb +9 -7
- data/lib/roda/version.rb +1 -1
- data/lib/roda.rb +37 -23
- metadata +7 -3
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The each_part plugin adds an each_part method, which is
|
|
7
|
-
# render_each
|
|
6
|
+
# The each_part plugin adds an +each_part+ method, which is similar to
|
|
7
|
+
# +render_each+, but treats all keywords as locals.
|
|
8
8
|
#
|
|
9
9
|
# # Can replace this:
|
|
10
10
|
# render_each(enum, :template, locals: {foo: 'bar'})
|
|
@@ -15,7 +15,7 @@ class Roda
|
|
|
15
15
|
# On Ruby 2.7+, the part method takes a keyword splat, so you
|
|
16
16
|
# must pass keywords and not a positional hash for the locals.
|
|
17
17
|
#
|
|
18
|
-
# If you are using the
|
|
18
|
+
# If you are using the +:assume_fixed_locals+ render plugin option,
|
|
19
19
|
# template caching is enabled, you are using Ruby 3+, and you
|
|
20
20
|
# are freezing your Roda application, in addition to providing a
|
|
21
21
|
# simpler API, this also provides a performance improvement.
|
|
@@ -38,11 +38,9 @@ class Roda
|
|
|
38
38
|
|
|
39
39
|
module InstanceMethods
|
|
40
40
|
if RUBY_VERSION >= '2.7'
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
end
|
|
45
|
-
RUBY
|
|
41
|
+
def each_part(enum, template, **locals, &block)
|
|
42
|
+
render_each(enum, template, :locals=>locals, &block)
|
|
43
|
+
end
|
|
46
44
|
# simplecov:disable
|
|
47
45
|
else
|
|
48
46
|
def each_part(enum, template, locals=OPTS, &block)
|
|
@@ -53,31 +51,27 @@ class Roda
|
|
|
53
51
|
end
|
|
54
52
|
|
|
55
53
|
module AssumeFixedLocalsInstanceMethods
|
|
56
|
-
# simplecov:disable
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
enum.each do |v|
|
|
66
|
-
locals[as] = v
|
|
67
|
-
yield send(optimized_method, **locals)
|
|
68
|
-
end
|
|
69
|
-
nil
|
|
70
|
-
else
|
|
71
|
-
enum.map do |v|
|
|
72
|
-
locals[as] = v
|
|
73
|
-
send(optimized_method, **locals)
|
|
74
|
-
end.join
|
|
54
|
+
if RUBY_VERSION >= '3.0' # simplecov:disable
|
|
55
|
+
def each_part(enum, template, **locals, &block)
|
|
56
|
+
if optimized_method = _cached_render_each_template_method(template)
|
|
57
|
+
optimized_method = optimized_method[0]
|
|
58
|
+
as = render_each_default_local(template)
|
|
59
|
+
if defined?(yield)
|
|
60
|
+
enum.each do |v|
|
|
61
|
+
locals[as] = v
|
|
62
|
+
yield send(optimized_method, **locals)
|
|
75
63
|
end
|
|
64
|
+
nil
|
|
76
65
|
else
|
|
77
|
-
|
|
66
|
+
enum.map do |v|
|
|
67
|
+
locals[as] = v
|
|
68
|
+
send(optimized_method, **locals)
|
|
69
|
+
end.join
|
|
78
70
|
end
|
|
71
|
+
else
|
|
72
|
+
render_each(enum, template, :locals=>locals, &block)
|
|
79
73
|
end
|
|
80
|
-
|
|
74
|
+
end
|
|
81
75
|
end
|
|
82
76
|
end
|
|
83
77
|
end
|
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The early_hints plugin
|
|
7
|
-
# using the rack.early_hints environment variable.
|
|
6
|
+
# The early_hints plugin adds the +send_early_hints+ method for sending
|
|
7
|
+
# 103 Early Hints responses using the +rack.early_hints+ environment variable.
|
|
8
8
|
# Early hints allow clients to preload necessary files before receiving
|
|
9
9
|
# the response.
|
|
10
10
|
module EarlyHints
|
|
11
11
|
module InstanceMethods
|
|
12
|
-
# Send given hash of Early Hints using the rack.early_hints environment variable,
|
|
13
|
-
# currenly
|
|
12
|
+
# Send given hash of Early Hints using the +rack.early_hints+ environment variable,
|
|
13
|
+
# currenly supported by puma and falcon. hash given should generally have the single
|
|
14
14
|
# key 'Link', and a string or array of strings for each of the early hints.
|
|
15
15
|
def send_early_hints(hash)
|
|
16
16
|
if eh_proc = env['rack.early_hints']
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The empty_root plugin makes +r.root+ match both
|
|
7
|
-
#
|
|
8
|
-
# rack applications, where the initial PATH_INFO has been moved
|
|
9
|
-
# to SCRIPT_NAME
|
|
6
|
+
# The empty_root plugin makes +r.root+ match both +/+ and
|
|
7
|
+
# the empty string. This is mostly useful when using multiple
|
|
8
|
+
# rack applications, where the initial +PATH_INFO+ has been moved
|
|
9
|
+
# to +SCRIPT_NAME+. For example, if you have the following
|
|
10
10
|
# applications:
|
|
11
11
|
#
|
|
12
12
|
# class App1 < Roda
|
|
@@ -28,7 +28,7 @@ class Roda
|
|
|
28
28
|
# Then requests for both +/albums/+ and +/albums+ will return
|
|
29
29
|
# "root". Without this plugin loaded into App2, only requests
|
|
30
30
|
# for +/albums/+ will return "root", since by default, +r.root+
|
|
31
|
-
# matches only when the current PATH_INFO is +/+ and not when
|
|
31
|
+
# matches only when the current +PATH_INFO+ is +/+ and not when
|
|
32
32
|
# it is empty.
|
|
33
33
|
module EmptyRoot
|
|
34
34
|
module RequestMethods
|
|
@@ -3,10 +3,10 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The environments plugin adds a environment class accessor to get
|
|
7
|
-
# the environment for the application,
|
|
8
|
-
# to check for the current environment (development
|
|
9
|
-
# production
|
|
6
|
+
# The environments plugin adds a +environment+ class accessor to get
|
|
7
|
+
# the environment for the application, predicate class methods
|
|
8
|
+
# to check for the current environment (+development?+, +test?+ and
|
|
9
|
+
# +production?+), and a class +configure+ method that takes environment(s)
|
|
10
10
|
# and yields to the block if the given environment(s) match the
|
|
11
11
|
# current environment.
|
|
12
12
|
#
|
|
@@ -40,30 +40,26 @@ class Roda
|
|
|
40
40
|
# end
|
|
41
41
|
# end
|
|
42
42
|
module Environments
|
|
43
|
-
# Set the environment to use for the app. Default to ENV['RACK_ENV']
|
|
44
|
-
# if no environment is given. If ENV['RACK_ENV'] is not set and
|
|
43
|
+
# Set the environment to use for the app. Default to <tt>ENV['RACK_ENV']</tt>
|
|
44
|
+
# if no environment is given. If <tt>ENV['RACK_ENV']</tt> is not set and
|
|
45
45
|
# no environment is given, assume the development environment.
|
|
46
46
|
def self.configure(app, env=ENV["RACK_ENV"])
|
|
47
|
-
app.environment = (env || 'development').to_sym
|
|
47
|
+
app.opts[:environment] = (env || 'development').to_sym
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
module ClassMethods
|
|
51
|
+
RodaPlugins.opt_attr_reader(self, :environment)
|
|
52
|
+
|
|
51
53
|
# If no environments are given or one of the given environments
|
|
52
54
|
# matches the current environment, yield the receiver to the block.
|
|
53
55
|
def configure(*envs)
|
|
54
|
-
if envs.empty? || envs.
|
|
56
|
+
if envs.empty? || envs.include?(environment)
|
|
55
57
|
yield self
|
|
56
58
|
end
|
|
57
59
|
end
|
|
58
60
|
|
|
59
|
-
# The current environment for the application, which should be stored
|
|
60
|
-
# as a symbol.
|
|
61
|
-
def environment
|
|
62
|
-
opts[:environment]
|
|
63
|
-
end
|
|
64
|
-
|
|
65
61
|
# Override the environment for the application, instead of using
|
|
66
|
-
# RACK_ENV
|
|
62
|
+
# <tt>ENV['RACK_ENV']</tt>.
|
|
67
63
|
def environment=(v)
|
|
68
64
|
opts[:environment] = v
|
|
69
65
|
end
|
data/lib/roda/plugins/erb_h.rb
CHANGED
|
@@ -12,9 +12,9 @@ class Roda
|
|
|
12
12
|
#
|
|
13
13
|
# To make sure that this speeds up applications using the h
|
|
14
14
|
# plugin, this depends on the h plugin, and overrides the
|
|
15
|
-
# h method.
|
|
15
|
+
# +h+ method.
|
|
16
16
|
#
|
|
17
|
-
# The following example will return "<foo>" as the body.
|
|
17
|
+
# The following example will return <tt>"<foo>"</tt> as the body.
|
|
18
18
|
#
|
|
19
19
|
# plugin :erb_h
|
|
20
20
|
#
|
|
@@ -25,8 +25,8 @@ class Roda
|
|
|
25
25
|
# The faster performance offered by the erb_h plugin is due
|
|
26
26
|
# to erb/escape avoiding allocations if not needed (returning the
|
|
27
27
|
# input object if no escaping is needed). That behavior change
|
|
28
|
-
# can cause problems if you mutate the result of the h method
|
|
29
|
-
# (which can mutate the input), or mutate the input of the h
|
|
28
|
+
# can cause problems if you mutate the result of the +h+ method
|
|
29
|
+
# (which can mutate the input), or mutate the input of the +h+
|
|
30
30
|
# method after calling it (which can mutate the result).
|
|
31
31
|
module ErbH
|
|
32
32
|
def self.load_dependencies(app)
|
|
@@ -5,8 +5,8 @@ require 'net/smtp'
|
|
|
5
5
|
class Roda
|
|
6
6
|
module RodaPlugins
|
|
7
7
|
# The error_email plugin adds an +error_email+ instance method that
|
|
8
|
-
#
|
|
9
|
-
#
|
|
8
|
+
# sends an email related to the exception. This is designed for use in
|
|
9
|
+
# combination with the error_handler plugin:
|
|
10
10
|
#
|
|
11
11
|
# plugin :error_email, to: 'to@example.com', from: 'from@example.com'
|
|
12
12
|
# plugin :error_handler do |e|
|
|
@@ -16,19 +16,19 @@ class Roda
|
|
|
16
16
|
#
|
|
17
17
|
# It is similar to the error_mail plugin, except that it uses net/smtp
|
|
18
18
|
# directly instead of using the mail library. If you are not already using the
|
|
19
|
-
# mail library in your application,
|
|
20
|
-
#
|
|
19
|
+
# mail library in your application, using error_email may be preferable over
|
|
20
|
+
# error_mail in terms of limiting the number of dependencies.
|
|
21
21
|
#
|
|
22
22
|
# Options:
|
|
23
23
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
24
|
+
# +:filter+ :: Callable called with the key and value for each parameter, environment
|
|
25
|
+
# variable, and session value. If it returns true, the value of the
|
|
26
|
+
# parameter is filtered in the email.
|
|
27
|
+
# +:from+ :: The From address to use in the email (required)
|
|
28
|
+
# +:headers+ :: A hash of additional headers to use in the email (default: empty hash)
|
|
29
|
+
# +:host+ :: The SMTP server to use to send the email (default: localhost)
|
|
30
|
+
# +:prefix+ :: A prefix to use in the email's subject line (default: no prefix)
|
|
31
|
+
# +:to+ :: The To address to use in the email (required)
|
|
32
32
|
#
|
|
33
33
|
# The subject of the error email shows the exception class and message.
|
|
34
34
|
# The body of the error email shows the backtrace of the error and the
|
|
@@ -56,7 +56,7 @@ class Roda
|
|
|
56
56
|
{'From'=>h[:from], 'To'=>h[:to], 'Subject'=>"#{h[:prefix]}#{subject}"}
|
|
57
57
|
end,
|
|
58
58
|
:body=>lambda do |s, e|
|
|
59
|
-
filter = s.
|
|
59
|
+
filter = s.class.error_email_opts[:filter]
|
|
60
60
|
format = lambda do |h|
|
|
61
61
|
h = h.map{|k, v| "#{k.inspect} => #{filter.call(k, v) ? 'FILTERED' : v.inspect}"}
|
|
62
62
|
h.sort!
|
|
@@ -111,17 +111,20 @@ END
|
|
|
111
111
|
end
|
|
112
112
|
}.freeze
|
|
113
113
|
|
|
114
|
-
# Set default opts for plugin. See ErrorEmail module RDoc for options.
|
|
115
114
|
def self.configure(app, opts=OPTS)
|
|
115
|
+
RodaPlugins.check_supported_opts("the error_email plugin", opts, [*DEFAULTS.keys, :prefix, :to, :from])
|
|
116
|
+
|
|
116
117
|
email_opts = app.opts[:error_email] ||= DEFAULTS
|
|
117
118
|
email_opts = email_opts.merge(opts)
|
|
118
|
-
email_opts[:headers] = email_opts[:headers].dup
|
|
119
|
+
email_opts[:headers] = email_opts[:headers].dup.freeze
|
|
119
120
|
unless email_opts[:to] && email_opts[:from]
|
|
120
121
|
raise RodaError, "must provide :to and :from options to error_email plugin"
|
|
121
122
|
end
|
|
122
|
-
app.opts[:error_email] = email_opts
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
app.opts[:error_email] = email_opts.freeze
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
module ClassMethods
|
|
127
|
+
RodaPlugins.opt_attr_reader(self, :error_email, name: :error_email_opts)
|
|
125
128
|
end
|
|
126
129
|
|
|
127
130
|
module InstanceMethods
|
|
@@ -129,7 +132,7 @@ END
|
|
|
129
132
|
# instance, but it can be a plain string which is used as the subject for
|
|
130
133
|
# the email.
|
|
131
134
|
def error_email(exception)
|
|
132
|
-
email_opts = self.class.
|
|
135
|
+
email_opts = self.class.error_email_opts.dup
|
|
133
136
|
email_opts[:message] = error_email_content(exception)
|
|
134
137
|
email_opts[:emailer].call(email_opts)
|
|
135
138
|
end
|
|
@@ -137,7 +140,7 @@ END
|
|
|
137
140
|
# The content of the email to send, include the headers and the body.
|
|
138
141
|
# Takes the same argument as #error_email.
|
|
139
142
|
def error_email_content(exception)
|
|
140
|
-
email_opts = self.class.
|
|
143
|
+
email_opts = self.class.error_email_opts
|
|
141
144
|
headers = email_opts[:default_headers].call(email_opts, exception)
|
|
142
145
|
headers = headers.merge(email_opts[:headers])
|
|
143
146
|
headers = headers.map{|k,v| "#{k}: #{v.gsub(/\r?\n/m, "\r\n ")}"}.sort.join("\r\n")
|
|
@@ -3,26 +3,19 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The error_handler plugin adds an error handler to the routing,
|
|
6
|
+
# The error_handler plugin adds an error handler to the routing tree,
|
|
7
7
|
# so that if routing the request raises an error, a nice error
|
|
8
|
-
# message page can be returned to the user.
|
|
8
|
+
# message page (or API response) can be returned to the user.
|
|
9
9
|
#
|
|
10
|
-
# You
|
|
10
|
+
# You should provide the error handler as a block to the plugin:
|
|
11
11
|
#
|
|
12
12
|
# plugin :error_handler do |e|
|
|
13
13
|
# "Oh No!"
|
|
14
14
|
# end
|
|
15
15
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
# error do |e|
|
|
21
|
-
# "Oh No!"
|
|
22
|
-
# end
|
|
23
|
-
#
|
|
24
|
-
# In both cases, the exception instance is passed into the block,
|
|
25
|
-
# and the block can return the request body via a string.
|
|
16
|
+
# If an error occurs, the exception instance is passed into the block,
|
|
17
|
+
# and the block can return the request body via a string (or another
|
|
18
|
+
# configured block result).
|
|
26
19
|
#
|
|
27
20
|
# If an exception is raised, a new response will be used, with the
|
|
28
21
|
# default status set to 500, before executing the error handler.
|
|
@@ -32,38 +25,40 @@ class Roda
|
|
|
32
25
|
# processing of that response occurs, except that an exception during
|
|
33
26
|
# after processing is logged to <tt>env['rack.errors']</tt> but
|
|
34
27
|
# otherwise ignored. This avoids recursive calls into the
|
|
35
|
-
#
|
|
28
|
+
# error handler. Note that if the error handler itself raises
|
|
36
29
|
# an exception, the exception will be raised without normal after
|
|
37
30
|
# processing. This can cause some after processing to run twice
|
|
38
|
-
# (once before the
|
|
31
|
+
# (once before the error handler is called and once after) if
|
|
39
32
|
# later after processing raises an exception.
|
|
40
33
|
#
|
|
41
34
|
# By default, this plugin handles StandardError and ScriptError.
|
|
42
|
-
# To override the exception classes it will handle, pass a
|
|
43
|
-
#
|
|
35
|
+
# To override the exception classes it will handle, pass a +classes+
|
|
36
|
+
# argument to the plugin:
|
|
44
37
|
#
|
|
45
38
|
# plugin :error_handler, classes: [StandardError, ScriptError, NoMemoryError]
|
|
46
39
|
module ErrorHandler
|
|
47
40
|
DEFAULT_ERROR_HANDLER_CLASSES = [StandardError, ScriptError].freeze
|
|
48
41
|
|
|
49
|
-
# If a block is given,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
app.opts[:error_handler_classes] = (
|
|
42
|
+
# If a block is given, use it to setup the error handler.
|
|
43
|
+
def self.configure(app, opts=(no_positional_opts = true; OPTS), classes: opts[:classes], **kw, &block)
|
|
44
|
+
RodaPlugins.check_keywords("the error_handler plugin", no_positional_opts, opts, kw)
|
|
45
|
+
app.opts[:error_handler_classes] = (classes || app.error_handler_classes || DEFAULT_ERROR_HANDLER_CLASSES).dup.freeze
|
|
53
46
|
|
|
54
47
|
if block
|
|
55
|
-
app.
|
|
48
|
+
app.class_exec do
|
|
49
|
+
define_method(:handle_error, &block)
|
|
50
|
+
alias_method(:handle_error, :handle_error)
|
|
51
|
+
private :handle_error
|
|
52
|
+
end
|
|
56
53
|
end
|
|
57
54
|
end
|
|
58
55
|
|
|
59
56
|
module ClassMethods
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
# the exception in the scope of the Roda instance.
|
|
57
|
+
RodaPlugins.opt_attr_reader(self, :error_handler_classes)
|
|
58
|
+
|
|
63
59
|
def error(&block)
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private :handle_error
|
|
60
|
+
RodaPlugins.warn("The error class method is deprecated, switch to passing the block when loading the error_handler plugin", uplevel: 1)
|
|
61
|
+
plugin(:error_handler, &block)
|
|
67
62
|
end
|
|
68
63
|
end
|
|
69
64
|
|
|
@@ -77,7 +72,7 @@ class Roda
|
|
|
77
72
|
ensure
|
|
78
73
|
_roda_after(res)
|
|
79
74
|
end
|
|
80
|
-
rescue *
|
|
75
|
+
rescue *self.class.error_handler_classes => e
|
|
81
76
|
_handle_error(e)
|
|
82
77
|
end
|
|
83
78
|
|
|
@@ -89,14 +84,14 @@ class Roda
|
|
|
89
84
|
ensure
|
|
90
85
|
_roda_after(res)
|
|
91
86
|
end
|
|
92
|
-
rescue *
|
|
87
|
+
rescue *self.class.error_handler_classes => e
|
|
93
88
|
_handle_error(e)
|
|
94
89
|
end
|
|
95
90
|
|
|
96
91
|
private
|
|
97
92
|
|
|
98
93
|
# Default empty implementation of _roda_after, usually
|
|
99
|
-
# overridden by Roda.
|
|
94
|
+
# overridden by Roda.def_roda_after.
|
|
100
95
|
def _roda_after(res)
|
|
101
96
|
end
|
|
102
97
|
|
|
@@ -119,9 +114,8 @@ class Roda
|
|
|
119
114
|
res
|
|
120
115
|
end
|
|
121
116
|
|
|
122
|
-
# By default, have the error handler reraise the error, so
|
|
123
|
-
# the plugin without
|
|
124
|
-
# behavior.
|
|
117
|
+
# By default, have the error handler reraise the error, so loading
|
|
118
|
+
# the plugin without a block doesn't change behavior.
|
|
125
119
|
def handle_error(e)
|
|
126
120
|
raise e
|
|
127
121
|
end
|
|
@@ -5,8 +5,8 @@ require 'mail'
|
|
|
5
5
|
class Roda
|
|
6
6
|
module RodaPlugins
|
|
7
7
|
# The error_mail plugin adds an +error_mail+ instance method that
|
|
8
|
-
# send an email related to the exception. This is
|
|
9
|
-
#
|
|
8
|
+
# send an email related to the exception. This is designed to be used
|
|
9
|
+
# with the error_handler plugin:
|
|
10
10
|
#
|
|
11
11
|
# plugin :error_mail, to: 'to@example.com', from: 'from@example.com'
|
|
12
12
|
# plugin :error_handler do |e|
|
|
@@ -21,13 +21,13 @@ class Roda
|
|
|
21
21
|
#
|
|
22
22
|
# Options:
|
|
23
23
|
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
24
|
+
# +:filter+ :: Callable called with the key and value for each parameter, environment
|
|
25
|
+
# variable, and session value. If it returns true, the value of the
|
|
26
|
+
# parameter is filtered in the email.
|
|
27
|
+
# +:from+ :: The From address to use in the email (required)
|
|
28
|
+
# +:headers+ :: A hash of additional headers to use in the email (default: empty hash)
|
|
29
|
+
# +:prefix+ :: A prefix to use in the email's subject line (default: no prefix)
|
|
30
|
+
# +:to+ :: The To address to use in the email (required)
|
|
31
31
|
#
|
|
32
32
|
# The subject of the error email shows the exception class and message.
|
|
33
33
|
# The body of the error email shows the backtrace of the error and the
|
|
@@ -42,12 +42,27 @@ class Roda
|
|
|
42
42
|
DEFAULT_FILTER = lambda{|k,v| false}
|
|
43
43
|
private_constant :DEFAULT_FILTER
|
|
44
44
|
|
|
45
|
+
PLUGIN_ARGUMENTS = %i[to from headers prefix filter].freeze
|
|
46
|
+
private_constant :PLUGIN_ARGUMENTS
|
|
47
|
+
|
|
48
|
+
Config = Struct.new(*PLUGIN_ARGUMENTS)
|
|
49
|
+
private_constant :Config
|
|
50
|
+
|
|
45
51
|
# Set default opts for plugin. See ErrorEmail module RDoc for options.
|
|
46
52
|
def self.configure(app, opts=OPTS)
|
|
47
|
-
|
|
53
|
+
RodaPlugins.check_supported_opts("the error_mail plugin", opts, PLUGIN_ARGUMENTS)
|
|
54
|
+
|
|
55
|
+
app.opts[:error_mail] = email_opts = (app.error_mail_opts || {:filter=>DEFAULT_FILTER}).merge(opts).freeze
|
|
48
56
|
unless email_opts[:to] && email_opts[:from]
|
|
49
57
|
raise RodaError, "must provide :to and :from options to error_mail plugin"
|
|
50
58
|
end
|
|
59
|
+
|
|
60
|
+
app.opts[:error_mail_config] = Config.new(*email_opts.values_at(*Config.members)).freeze
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
module ClassMethods
|
|
64
|
+
RodaPlugins.opt_attr_reader(self, :error_mail, name: :error_mail_opts)
|
|
65
|
+
RodaPlugins.opt_attr_reader(self, :error_mail_config)
|
|
51
66
|
end
|
|
52
67
|
|
|
53
68
|
module InstanceMethods
|
|
@@ -67,14 +82,14 @@ class Roda
|
|
|
67
82
|
private
|
|
68
83
|
|
|
69
84
|
def _error_mail(e)
|
|
70
|
-
|
|
85
|
+
config = self.class.error_mail_config
|
|
71
86
|
subject = if e.respond_to?(:message)
|
|
72
87
|
"#{e.class}: #{e.message}"
|
|
73
88
|
else
|
|
74
89
|
e.to_s
|
|
75
90
|
end
|
|
76
|
-
subject = "#{
|
|
77
|
-
filter =
|
|
91
|
+
subject = "#{config.prefix}#{subject}"
|
|
92
|
+
filter = config.filter
|
|
78
93
|
|
|
79
94
|
format = lambda do |h|
|
|
80
95
|
h = h.map{|k, v| "#{k.inspect} => #{filter.call(k, v) ? 'FILTERED' : v.inspect}"}
|
|
@@ -129,12 +144,12 @@ END
|
|
|
129
144
|
|
|
130
145
|
|
|
131
146
|
Mail.new do
|
|
132
|
-
from
|
|
133
|
-
to
|
|
147
|
+
from config.from
|
|
148
|
+
to config.to
|
|
134
149
|
subject subject
|
|
135
150
|
body message
|
|
136
151
|
|
|
137
|
-
if headers =
|
|
152
|
+
if headers = config.headers
|
|
138
153
|
headers.each do |k,v|
|
|
139
154
|
header[k] = v
|
|
140
155
|
end
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
#
|
|
4
4
|
class Roda
|
|
5
5
|
module RodaPlugins
|
|
6
|
-
# The exception_page plugin provides an exception_page method that is designed
|
|
6
|
+
# The exception_page plugin provides an +exception_page+ method that is designed
|
|
7
7
|
# to be called inside the error handler to provide a page to the developer
|
|
8
8
|
# with debugging information. It should only be used in developer environments
|
|
9
9
|
# with trusted clients, as it can leak source code and other information that
|
|
@@ -21,29 +21,29 @@ class Roda
|
|
|
21
21
|
# differences:
|
|
22
22
|
#
|
|
23
23
|
# * Not a middleware, so it doesn't handle exceptions itself, and has no effect
|
|
24
|
-
# on the callstack unless the exception_page method is called.
|
|
24
|
+
# on the callstack unless the +exception_page+ method is called.
|
|
25
25
|
# * Supports external javascript and stylesheets, allowing context toggling to
|
|
26
26
|
# work in applications that use a content security policy to restrict inline
|
|
27
|
-
# javascript and stylesheets (
|
|
28
|
-
# * Has fewer dependencies (does not require
|
|
27
|
+
# javascript and stylesheets (+assets+, +css_file+, and +js_file+ arguments).
|
|
28
|
+
# * Has fewer dependencies (does not require erb).
|
|
29
29
|
# * Sets the Content-Type for the response, and returns the body string, but does
|
|
30
30
|
# not modify other headers or the response status.
|
|
31
|
-
# * Supports a configurable amount of context lines in backtraces (
|
|
32
|
-
# * Supports optional JSON formatted output, if used with the json plugin (
|
|
31
|
+
# * Supports a configurable amount of context lines in backtraces (+context+ argument).
|
|
32
|
+
# * Supports optional JSON formatted output, if used with the json plugin (+json+ argument).
|
|
33
33
|
#
|
|
34
|
-
# To
|
|
35
|
-
# in your routing tree:
|
|
34
|
+
# To handle requests for the external javascript and stylesheets, you can call
|
|
35
|
+
# +r.exception_page_assets+ in your routing tree:
|
|
36
36
|
#
|
|
37
37
|
# route do |r|
|
|
38
38
|
# # ...
|
|
39
39
|
#
|
|
40
40
|
# # serve GET /exception_page.{css,js} requests
|
|
41
|
-
# # Use with assets: true
|
|
41
|
+
# # Use with exception_page assets: true argument
|
|
42
42
|
# r.exception_page_assets
|
|
43
43
|
#
|
|
44
44
|
# r.on "static" do
|
|
45
45
|
# # serve GET /static/exception_page.{css,js} requests
|
|
46
|
-
# # Use with assets: '/static'
|
|
46
|
+
# # Use with exception_page assets: '/static' option
|
|
47
47
|
# r.exception_page_assets
|
|
48
48
|
# end
|
|
49
49
|
# end
|
|
@@ -175,29 +175,37 @@ END
|
|
|
175
175
|
# is not accepted, then just show a plain text page with the exception
|
|
176
176
|
# class, message, and backtrace.
|
|
177
177
|
#
|
|
178
|
-
#
|
|
178
|
+
# Arguments:
|
|
179
179
|
#
|
|
180
|
-
#
|
|
181
|
-
#
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
#
|
|
186
|
-
#
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
#
|
|
198
|
-
def exception_page(exception, opts=OPTS)
|
|
180
|
+
# +assets+ :: If +true+, sets :css_file to +/exception_page.css+ and :js_file to
|
|
181
|
+
# +/exception_page.js+, assuming that +r.exception_page_assets+ is called
|
|
182
|
+
# in the route block to serve the exception page assets. If a String,
|
|
183
|
+
# uses the string as a prefix, assuming that +r.exception_page_assets+
|
|
184
|
+
# is called in a nested block inside the route block. If false, doesn't
|
|
185
|
+
# use any CSS or JS.
|
|
186
|
+
# +context+ :: The number of context lines before and after each line in
|
|
187
|
+
# the backtrace (default: 7).
|
|
188
|
+
# +css_file+ :: A path to the external CSS file for the HTML exception page. If false,
|
|
189
|
+
# doesn't use any CSS.
|
|
190
|
+
# +js_file+ :: A path to the external javascript file for the HTML exception page. If
|
|
191
|
+
# false, doesn't use any JS.
|
|
192
|
+
# +json+ :: Return a hash of exception information. The hash will have
|
|
193
|
+
# a single key, <tt>"exception"</tt>, with a value being a hash with
|
|
194
|
+
# three keys, <tt>"class"</tt>, <tt>"message"</tt>, and <tt>"backtrace"</tt>, which
|
|
195
|
+
# contain information derived from the given exception.
|
|
196
|
+
# Designed to be used with the +json+ plugin, which will
|
|
197
|
+
# automatically convert the hash to JSON format.
|
|
198
|
+
def exception_page(exception, opts=(no_positional_opts = true; OPTS),
|
|
199
|
+
assets: opts[:assets],
|
|
200
|
+
context: opts[:context],
|
|
201
|
+
css_file: opts[:css_file],
|
|
202
|
+
js_file: opts[:js_file],
|
|
203
|
+
json: opts[:json],
|
|
204
|
+
**kw)
|
|
205
|
+
RodaPlugins.check_keywords(__callee__, no_positional_opts, opts, kw)
|
|
206
|
+
|
|
199
207
|
message = exception_page_exception_message(exception)
|
|
200
|
-
if
|
|
208
|
+
if json
|
|
201
209
|
@_response[RodaResponseHeaders::CONTENT_TYPE] = "application/json"
|
|
202
210
|
{
|
|
203
211
|
"exception"=>{
|
|
@@ -209,11 +217,9 @@ END
|
|
|
209
217
|
elsif env['HTTP_ACCEPT'] =~ /text\/html/
|
|
210
218
|
@_response[RodaResponseHeaders::CONTENT_TYPE] = "text/html"
|
|
211
219
|
|
|
212
|
-
context
|
|
213
|
-
css_file = opts[:css_file]
|
|
214
|
-
js_file = opts[:js_file]
|
|
220
|
+
context ||= 7
|
|
215
221
|
|
|
216
|
-
case prefix =
|
|
222
|
+
case prefix = assets
|
|
217
223
|
when false
|
|
218
224
|
css_file = false if css_file.nil?
|
|
219
225
|
js_file = false if js_file.nil?
|