<?php

declare(strict_types=1);

namespace Drupal\flowdrop_stategraph\Exception;

use Drupal\flowdrop\Exception\FlowDropException;

/**
 * Exception thrown when state validation fails.
 *
 * This exception is thrown when:
 * - Required state fields are missing.
 * - State data fails type validation.
 * - State deserialization fails.
 *
 * @api
 */
class StateValidationException extends FlowDropException {

  /**
   * Constructs a new StateValidationException.
   *
   * @param string $message
   *   The error message.
   * @param array<string> $validationErrors
   *   List of specific validation errors.
   * @param \Throwable|null $previous
   *   Previous exception for chaining.
   */
  public function __construct(
    string $message,
    public readonly array $validationErrors = [],
    ?\Throwable $previous = NULL,
  ) {
    if (!empty($validationErrors)) {
      $message .= " Errors: " . implode("; ", $validationErrors);
    }

    parent::__construct($message, 0, $previous);
  }

}
