Exception: Synthra::Errors::UniquenessError

Inherits:
GenerationError show all
Defined in:
lib/synthra/errors.rb

Overview

Uniqueness constraint could not be satisfied

Raised when the generator cannot produce a unique value after multiple attempts. This typically happens when generating many records with unique fields that have limited possible values.

Examples:

# When generating 1000 users with unique emails from small domain
# UniquenessError: Could not generate unique value for 'email' after 100 attempts

Constant Summary collapse

ERROR_CODE =
"UNIQUENESS_ERROR"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field:, attempts: nil, last_values: nil, schema_name: nil) ⇒ UniquenessError

Create a new UniquenessError

Parameters:

  • field (String)

    the field name that requires uniqueness

  • attempts (Integer) (defaults to: nil)

    number of generation attempts made

  • last_values (Array) (defaults to: nil)

    the last values attempted (for debugging)

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

    the schema name where the error occurred



639
640
641
642
643
644
645
646
647
648
649
# File 'lib/synthra/errors.rb', line 639

def initialize(field:, attempts: nil, last_values: nil, schema_name: nil)
  @field = field
  @attempts = attempts || Synthra.configuration.max_unique_retries
  @last_values = last_values || []
  @schema_name = schema_name
  message = "Could not generate unique value for '#{field}'"
  message += " in schema '#{schema_name}'" if schema_name
  message += " after #{@attempts} attempts"
  message += ". Last values: #{@last_values.first(5).inspect}" unless @last_values.empty?
  super(message)
end

Instance Attribute Details

#attemptsInteger (readonly)

Returns number of attempts made before giving up.

Returns:

  • (Integer)

    number of attempts made before giving up



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/synthra/errors.rb', line 626

class UniquenessError < GenerationError
  ERROR_CODE = "UNIQUENESS_ERROR"
  attr_reader :field, :attempts, :last_values, :schema_name


  # Create a new UniquenessError
  #
  # @param field [String] the field name that requires uniqueness
  # @param attempts [Integer] number of generation attempts made
  # @param last_values [Array] the last values attempted (for debugging)
  # @param schema_name [String, nil] the schema name where the error occurred
  #

  def initialize(field:, attempts: nil, last_values: nil, schema_name: nil)
    @field = field
    @attempts = attempts || Synthra.configuration.max_unique_retries
    @last_values = last_values || []
    @schema_name = schema_name
    message = "Could not generate unique value for '#{field}'"
    message += " in schema '#{schema_name}'" if schema_name
    message += " after #{@attempts} attempts"
    message += ". Last values: #{@last_values.first(5).inspect}" unless @last_values.empty?
    super(message)
  end
end

#fieldString (readonly)

Returns the field name that requires uniqueness.

Returns:

  • (String)

    the field name that requires uniqueness



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/synthra/errors.rb', line 626

class UniquenessError < GenerationError
  ERROR_CODE = "UNIQUENESS_ERROR"
  attr_reader :field, :attempts, :last_values, :schema_name


  # Create a new UniquenessError
  #
  # @param field [String] the field name that requires uniqueness
  # @param attempts [Integer] number of generation attempts made
  # @param last_values [Array] the last values attempted (for debugging)
  # @param schema_name [String, nil] the schema name where the error occurred
  #

  def initialize(field:, attempts: nil, last_values: nil, schema_name: nil)
    @field = field
    @attempts = attempts || Synthra.configuration.max_unique_retries
    @last_values = last_values || []
    @schema_name = schema_name
    message = "Could not generate unique value for '#{field}'"
    message += " in schema '#{schema_name}'" if schema_name
    message += " after #{@attempts} attempts"
    message += ". Last values: #{@last_values.first(5).inspect}" unless @last_values.empty?
    super(message)
  end
end

#last_valuesArray (readonly)

Returns the last few values that were rejected as duplicates.

Returns:

  • (Array)

    the last few values that were rejected as duplicates



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/synthra/errors.rb', line 626

class UniquenessError < GenerationError
  ERROR_CODE = "UNIQUENESS_ERROR"
  attr_reader :field, :attempts, :last_values, :schema_name


  # Create a new UniquenessError
  #
  # @param field [String] the field name that requires uniqueness
  # @param attempts [Integer] number of generation attempts made
  # @param last_values [Array] the last values attempted (for debugging)
  # @param schema_name [String, nil] the schema name where the error occurred
  #

  def initialize(field:, attempts: nil, last_values: nil, schema_name: nil)
    @field = field
    @attempts = attempts || Synthra.configuration.max_unique_retries
    @last_values = last_values || []
    @schema_name = schema_name
    message = "Could not generate unique value for '#{field}'"
    message += " in schema '#{schema_name}'" if schema_name
    message += " after #{@attempts} attempts"
    message += ". Last values: #{@last_values.first(5).inspect}" unless @last_values.empty?
    super(message)
  end
end

#schema_nameObject (readonly)

Returns the value of attribute schema_name.



628
629
630
# File 'lib/synthra/errors.rb', line 628

def schema_name
  @schema_name
end