You are here

public function FrontMatterParseException::__construct in Drupal 10

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/FrontMatter/Exception/FrontMatterParseException.php \Drupal\Component\FrontMatter\Exception\FrontMatterParseException::__construct()

Constructs a new FrontMatterParseException instance.

Parameters

\Drupal\Component\Serialization\Exception\InvalidDataTypeException $exception: The exception thrown when attempting to parse front matter data.

File

core/lib/Drupal/Component/FrontMatter/Exception/FrontMatterParseException.php, line 30

Class

FrontMatterParseException
Defines a class for front matter parsing exceptions.

Namespace

Drupal\Component\FrontMatter\Exception

Code

public function __construct(InvalidDataTypeException $exception) {
  $this->sourceLine = 1;

  // Attempt to extract the line number from the serializer error. This isn't
  // a very stable way to do this, however it is the only way given that
  // \Drupal\Component\Serialization\SerializationInterface does not have
  // methods for accessing this kind of information reliably.
  $message = 'An error occurred when attempting to parse front matter data';
  if ($exception) {
    preg_match('/line:?\\s?(\\d+)/i', $exception
      ->getMessage(), $matches);
    if (!empty($matches[1])) {
      $message .= ' on line %d';

      // Add any matching line count to the existing source line so it
      // increases it by 1 to account for the front matter separator (---).
      $this->sourceLine += (int) $matches[1];
    }
  }
  parent::__construct(sprintf($message, $this->sourceLine), 0, $exception);
}