Exception class for errors that are due to the submitted parameters not matching what is expected. Should probably be treated as a 4xx error.
Methods
Public Class
Public Instance
Attributes
all_errors | [W] |
An array of all other errors that were raised with this error. If the error was not raised inside This allows you to use |
keys | [RW] |
The keys used to access the parameter that caused the error. This is an array that can be splatted to |
reason | [RW] |
The reason behind this error. If this error was caused by a conversion method, this will be the conversion method symbol. If this error was caused because a value was missing, then it will be |
Public Class methods
Set the keys in the given exception. If the exception is not already an instance of the class, create a new instance to wrap it.
# File lib/roda/plugins/typecast_params.rb 316 def self.create(keys, reason, e) 317 if e.is_a?(self) 318 e.keys ||= keys 319 e.reason ||= reason 320 e 321 else 322 backtrace = e.backtrace 323 e = new("#{e.class}: #{e.message}") 324 e.keys = keys 325 e.reason = reason 326 e.set_backtrace(backtrace) if backtrace 327 e 328 end 329 end
Public Instance methods
# File lib/roda/plugins/typecast_params.rb 344 def all_errors 345 @all_errors ||= [self] 346 end
The likely parameter name where the contents were not expected. This is designed for cases where the parameter was submitted with the typical application/x-www-form-urlencoded or multipart/form-data content types, and assumes the typical rack parsing of these content types into parameters. # If the parameters were submitted via JSON, keys
should be used directly.
Example:
# keys: ['page'] param_name => 'page' # keys: ['artist', 'name'] param_name => 'artist[name]' # keys: ['album', 'artist', 'name'] param_name => 'album[artist][name]'
# File lib/roda/plugins/typecast_params.rb 371 def param_name 372 if keys.length > 1 373 first, *rest = keys 374 v = first.dup 375 rest.each do |param| 376 v << "[" 377 v << param unless param.is_a?(Integer) 378 v << "]" 379 end 380 v 381 else 382 keys.first 383 end 384 end
An array of all parameter names for parameters where the context were not expected. If Params#convert!
was not used, this will be an array containing param_name
. If Params#convert!
was used and multiple exceptions were captured inside the convert! block, this will contain the parameter names related to all captured exceptions.
# File lib/roda/plugins/typecast_params.rb 391 def param_names 392 all_errors.map(&:param_name) 393 end