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
29 * @param string $message The error message
30 * @param int $parsedLine The line where the error occurred
31 * @param int $snippet The snippet of code near the problem
32 * @param string $parsedFile The file name where the error occurred
33 * @param \Exception $previous The previous exception
35 public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, \Exception $previous = null)
37 $this->parsedFile = $parsedFile;
38 $this->parsedLine = $parsedLine;
39 $this->snippet = $snippet;
40 $this->rawMessage = $message;
44 parent::__construct($this->message, 0, $previous);
48 * Gets the snippet of code near the error.
50 * @return string The snippet of code
52 public function getSnippet()
54 return $this->snippet;
58 * Sets the snippet of code near the error.
60 * @param string $snippet The code snippet
62 public function setSnippet($snippet)
64 $this->snippet = $snippet;
70 * Gets the filename where the error occurred.
72 * This method returns null if a string is parsed.
74 * @return string The filename
76 public function getParsedFile()
78 return $this->parsedFile;
82 * Sets the filename where the error occurred.
84 * @param string $parsedFile The filename
86 public function setParsedFile($parsedFile)
88 $this->parsedFile = $parsedFile;
94 * Gets the line where the error occurred.
96 * @return int The file line
98 public function getParsedLine()
100 return $this->parsedLine;
104 * Sets the line where the error occurred.
106 * @param int $parsedLine The file line
108 public function setParsedLine($parsedLine)
110 $this->parsedLine = $parsedLine;
115 private function updateRepr()
117 $this->message = $this->rawMessage;
120 if ('.' === substr($this->message, -1)) {
121 $this->message = substr($this->message, 0, -1);
125 if (null !== $this->parsedFile) {
126 if (PHP_VERSION_ID >= 50400) {
127 $jsonOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
131 $this->message .= sprintf(' in %s', json_encode($this->parsedFile, $jsonOptions));
134 if ($this->parsedLine >= 0) {
135 $this->message .= sprintf(' at line %d', $this->parsedLine);
138 if ($this->snippet) {
139 $this->message .= sprintf(' (near "%s")', $this->snippet);
143 $this->message .= '.';