Class: Synthra::Behaviors::SimulateError

Inherits:
Base
  • Object
show all
Defined in:
lib/synthra/behaviors/simulate_error.rb

Overview

Simulate error behavior - raises HTTP-like error

Raises SimulatedError exception with a specified HTTP status code when activated, simulating API errors.

Examples:

Apply simulate error behavior

behavior = SimulateError.new(500, rng)  # 500 error
begin
  behavior.apply({ "name" => "John" })
rescue SimulatedError => e
  e.code  # => 500
end

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Synthra::Behaviors::Base

Instance Method Details

#apply(result, context = nil) ⇒ Object

Apply simulate error behavior

Raises SimulatedError if the behavior should be applied based on probability.

Parameters:

  • result (Object)

    generated data (not modified)

  • context (Generator::Context, nil) (defaults to: nil)

    generation context (not used)

Returns:

  • (Object)

    result (if behavior not applied)

Raises:



50
51
52
53
54
55
56
57
# File 'lib/synthra/behaviors/simulate_error.rb', line 50

def apply(result, context = nil)
  if should_apply?
    code = extract_code
    probability = extract_probability
    raise SimulatedError.new(code: code, probability: probability)
  end
  result
end

#extract_codeInteger (private)

Extract error code from value

Extracts the HTTP status code from the behavior value configuration.

Returns:

  • (Integer)

    HTTP status code (default: 500)



69
70
71
72
73
74
75
# File 'lib/synthra/behaviors/simulate_error.rb', line 69

def extract_code
  case value
  when Integer then value
  when Hash then value[:code] || 500
  else 500
  end
end