Exception: CSVPlusPlus::Error::ModifierValidationError

Inherits:
Error
  • Object
show all
Defined in:
lib/csv_plus_plus/error/modifier_validation_error.rb

Overview

An error that can be thrown when a modifier doesn’t pass our validation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifier, bad_input, choices: nil, message: nil) ⇒ ModifierValidationError

You must supply either a choices or message

Parameters:

  • modifier (Symbol)

    The modifier being parsed when the bad input was encountered

  • bad_input (String)

    The offending input that caused the error to be thrown

  • choices (Array<Symbol>, nil) (defaults to: nil)

    The choices that value must be one of (but violated)

  • message (String, nil) (defaults to: nil)

    A relevant message to show



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 22

def initialize(modifier, bad_input, choices: nil, message: nil)
  @bad_input = bad_input
  @choices = choices
  @modifier = modifier

  @message =
    if @choices
      "must be one of (#{@choices.map(&:to_s).join(', ')})"
    else
      message
    end

  super(@message)
end

Instance Attribute Details

#bad_inputString (readonly)

The offending input that caused the error to be thrown

Returns:

  • (String)

    the current value of bad_input



13
14
15
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 13

def bad_input
  @bad_input
end

#choicesArray<Symbol>? (readonly)

The choices that value must be one of (but violated)

Returns:

  • (Array<Symbol>, nil)

    the current value of choices



13
14
15
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 13

def choices
  @choices
end

#messageString? (readonly)

A relevant message to show

Returns:

  • (String, nil)

    the current value of message



13
14
15
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 13

def message
  @message
end

#modifierSymbol (readonly)

The modifier being parsed when the bad input was encountered

Returns:

  • (Symbol)

    the current value of modifier



13
14
15
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 13

def modifier
  @modifier
end

Instance Method Details

#error_message::String

Create a relevant error message given @choices or @message (one of them must be supplied).

Returns:

  • (::String)


40
41
42
43
44
45
46
# File 'lib/csv_plus_plus/error/modifier_validation_error.rb', line 40

def error_message
  <<~ERROR_MESSAGE
    Error parsing modifier: [[#{@modifier}=...]]
    Bad input: #{@bad_input}
    Reason: #{@message}
  ERROR_MESSAGE
end