private function enhanceTwigException(\Twig_Error $e, Document $document, $template = null)
{
if (-1 === $e->getTemplateLine()) {
return new \RuntimeException($e->getRawMessage());
}
$lines = explode(PHP_EOL, $document->getBody());
if (!$template || $template->getTemplateName() == $e->getTemplateFile()) {
return new \RuntimeException(OutputFormatter::escape(sprintf('%s near "%s" near line %d.', $e->getRawMessage(), $lines[$e->getTemplateLine() - 1], $e->getTemplateLine())));
}
return new \RuntimeException(OutputFormatter::escape(sprintf('%s in a string template line %d.', $e->getRawMessage(), $e->getTemplateLine())));
}