4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Yaml\Exception;
15 * Exception class thrown when an error occurs during parsing.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ParseException extends RuntimeException
27 * @param string $message The error message
28 * @param int $parsedLine The line where the error occurred
29 * @param string|null $snippet The snippet of code near the problem
30 * @param string|null $parsedFile The file name where the error occurred
31 * @param \Exception|null $previous The previous exception
33 public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
35 $this->parsedFile = $parsedFile;
36 $this->parsedLine = $parsedLine;
37 $this->snippet = $snippet;
38 $this->rawMessage = $message;
42 parent::__construct($this->message, 0, $previous);
46 * Gets the snippet of code near the error.
48 * @return string The snippet of code
50 public function getSnippet()
52 return $this->snippet;
56 * Sets the snippet of code near the error.
58 * @param string $snippet The code snippet
60 public function setSnippet($snippet)
62 $this->snippet = $snippet;
68 * Gets the filename where the error occurred.
70 * This method returns null if a string is parsed.
72 * @return string The filename
74 public function getParsedFile()
76 return $this->parsedFile;
80 * Sets the filename where the error occurred.
82 * @param string $parsedFile The filename
84 public function setParsedFile($parsedFile)
86 $this->parsedFile = $parsedFile;
92 * Gets the line where the error occurred.
94 * @return int The file line
96 public function getParsedLine()
98 return $this->parsedLine;
102 * Sets the line where the error occurred.
104 * @param int $parsedLine The file line
106 public function setParsedLine($parsedLine)
108 $this->parsedLine = $parsedLine;
113 private function updateRepr()
115 $this->message = $this->rawMessage;
118 if ('.' === substr($this->message, -1)) {
119 $this->message = substr($this->message, 0, -1);
123 if (null !== $this->parsedFile) {
124 $this->message .= sprintf(' in %s', json_encode($this->parsedFile, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
127 if ($this->parsedLine >= 0) {
128 $this->message .= sprintf(' at line %d', $this->parsedLine);
131 if ($this->snippet) {
132 $this->message .= sprintf(' (near "%s")', $this->snippet);
136 $this->message .= '.';