Exception: Roda::RodaPlugins::TypecastParams::Error

Inherits:
Roda::RodaError
  • Object
show all
Defined in:
lib/roda/plugins/typecast_params.rb

Overview

Exception class for errors that are due to the submitted parameters not matching what is expected. Should probably be treated as a 4xx error.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#all_errorsObject



307
308
309
# File 'lib/roda/plugins/typecast_params.rb', line 307

def all_errors
  @all_errors ||= [self]
end

#keysObject

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.



296
297
298
# File 'lib/roda/plugins/typecast_params.rb', line 296

def keys
  @keys
end

#reasonObject

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.



315
316
317
# File 'lib/roda/plugins/typecast_params.rb', line 315

def reason
  @reason
end

Class Method Details

.create(keys, reason, e) ⇒ Object

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.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/roda/plugins/typecast_params.rb', line 279

def self.create(keys, reason, e)
  if e.is_a?(self)
    e.keys ||= keys
    e.reason ||= reason
    e
  else
    backtrace = e.backtrace
    e = new("#{e.class}: #{e.message}")
    e.keys = keys
    e.reason = reason
    e.set_backtrace(backtrace) if backtrace
    e
  end
end

Instance Method Details

#param_nameObject

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]'


334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/roda/plugins/typecast_params.rb', line 334

def param_name
  if keys.length > 1
    first, *rest = keys
    v = first.dup
    rest.each do |param|
      v << "["
      v << param unless param.is_a?(Integer)
      v << "]"
    end
    v
  else
    keys.first
  end
end

#param_namesObject

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.



354
355
356
# File 'lib/roda/plugins/typecast_params.rb', line 354

def param_names
  all_errors.map(&:param_name)
end