Skip to content

Commit 7ebd16d

Browse files
author
Kevin Hamer
committed
Adding ->showErrorsAsCSS() to Leafo\ScssPhp\Server
* enables displaying errors as a psuedo element via CSS
1 parent 75db452 commit 7ebd16d

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/Server.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
class Server
2323
{
24+
protected $showErrorsAsCSS = false;
25+
2426
/**
2527
* Join path components
2628
*
@@ -203,6 +205,35 @@ protected function compile($in, $out)
203205
return array($css, $etag);
204206
}
205207

208+
protected function createErrorCSS($error)
209+
{
210+
$message = str_replace(
211+
array("'", "\n"),
212+
array("\\'", "\\A"),
213+
$error->getfile() . ":\n\n" . $error->getMessage()
214+
);
215+
216+
return "body { display: none !important; }
217+
html:after {
218+
background: white;
219+
color: black;
220+
content: '$message';
221+
display: block !important;
222+
font-family: mono;
223+
padding: 1em;
224+
white-space: pre;
225+
}";
226+
}
227+
228+
/**
229+
* Render errors as a psuedo-element within valid CSS, displaying the errors on any
230+
* page that includes this CSS.
231+
*/
232+
public function showErrorsAsCSS()
233+
{
234+
$this->showErrorsAsCSS = true;
235+
}
236+
206237
/**
207238
* Compile requested scss and serve css. Outputs HTTP response.
208239
*
@@ -231,10 +262,15 @@ public function serve($salt = '')
231262
echo $css;
232263

233264
} catch (\Exception $e) {
234-
header($protocol . ' 500 Internal Server Error');
235-
header('Content-type: text/plain');
265+
if ($this->showErrorsAsCSS) {
266+
header('Content-type: text/css');
267+
echo $this->createErrorCSS($e);
268+
} else {
269+
header($protocol . ' 500 Internal Server Error');
270+
header('Content-type: text/plain');
271+
echo 'Parse error: ' . $e->getMessage() . "\n";
272+
}
236273

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

240276
return;

0 commit comments

Comments
 (0)