class Roda::RodaPlugins::TypecastParams::Error

  1. lib/roda/plugins/typecast_params.rb
Superclass: RodaError

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

  1. create

Public Instance

  1. all_errors
  2. all_errors
  3. keys
  4. param_name
  5. param_names
  6. reason

Attributes

all_errors [W]

An array of all other errors that were raised with this error. If the error was not raised inside Params#convert! or Params#convert_each!, this will just be an array containing the current receiver.

This allows you to use Params#convert! to process a form input, and if any conversion errors occur inside the block, it can provide an array of all parameter names and reasons for parameters with problems.

keys [RW]

The keys used to access the parameter that caused the error. This is an array that can be splatted to dig to get the value of the parameter causing the error.

reason [RW]

The reason behind this error. If this error was caused by a conversion method, this will be the the conversion method symbol. If this error was caused because a value was missing, then it will be :missing. If this error was caused because a value was not the correct type, then it will be :invalid_type.

Public Class methods

create(keys, reason, e)

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.

[show source]
    # File lib/roda/plugins/typecast_params.rb
284 def self.create(keys, reason, e)
285   if e.is_a?(self)
286     e.keys ||= keys
287     e.reason ||= reason
288     e
289   else
290     backtrace = e.backtrace
291     e = new("#{e.class}: #{e.message}")
292     e.keys = keys
293     e.reason = reason
294     e.set_backtrace(backtrace) if backtrace
295     e
296   end
297 end

Public Instance methods

all_errors()
[show source]
    # File lib/roda/plugins/typecast_params.rb
312 def all_errors
313   @all_errors ||= [self]
314 end
param_name()

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]'
[show source]
    # File lib/roda/plugins/typecast_params.rb
339 def param_name
340   if keys.length > 1
341     first, *rest = keys
342     v = first.dup
343     rest.each do |param|
344       v << "["
345       v << param unless param.is_a?(Integer)
346       v << "]"
347     end
348     v
349   else
350     keys.first
351   end
352 end
param_names()

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.

[show source]
    # File lib/roda/plugins/typecast_params.rb
359 def param_names
360   all_errors.map(&:param_name)
361 end