Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class Server
{
protected $showErrorsAsCSS = false;

/**
* Join path components
*
Expand Down Expand Up @@ -203,6 +205,35 @@ protected function compile($in, $out)
return array($css, $etag);
}

protected function createErrorCSS($error)
{
$message = str_replace(
array("'", "\n"),
array("\\'", "\\A"),
$error->getfile() . ":\n\n" . $error->getMessage()
);

return "body { display: none !important; }
html:after {
background: white;
color: black;
content: '$message';
display: block !important;
font-family: mono;
padding: 1em;
white-space: pre;
}";
}

/**
* Render errors as a psuedo-element within valid CSS, displaying the errors on any
* page that includes this CSS.
*/
public function showErrorsAsCSS()
{
$this->showErrorsAsCSS = true;
}

/**
* Compile requested scss and serve css. Outputs HTTP response.
*
Expand Down Expand Up @@ -231,10 +262,15 @@ public function serve($salt = '')
echo $css;

} catch (\Exception $e) {
header($protocol . ' 500 Internal Server Error');
header('Content-type: text/plain');
if ($this->showErrorsAsCSS) {
header('Content-type: text/css');
echo $this->createErrorCSS($e);
} else {
header($protocol . ' 500 Internal Server Error');
header('Content-type: text/plain');
echo 'Parse error: ' . $e->getMessage() . "\n";
}

echo 'Parse error: ' . $e->getMessage() . "\n";
}

return;
Expand Down