|
21 | 21 | */ |
22 | 22 | class Server |
23 | 23 | { |
| 24 | + protected $showErrorsAsCSS = false; |
| 25 | + |
24 | 26 | /** |
25 | 27 | * Join path components |
26 | 28 | * |
@@ -203,6 +205,35 @@ protected function compile($in, $out) |
203 | 205 | return array($css, $etag); |
204 | 206 | } |
205 | 207 |
|
| 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 | + |
206 | 237 | /** |
207 | 238 | * Compile requested scss and serve css. Outputs HTTP response. |
208 | 239 | * |
@@ -231,10 +262,15 @@ public function serve($salt = '') |
231 | 262 | echo $css; |
232 | 263 |
|
233 | 264 | } 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 | + } |
236 | 273 |
|
237 | | - echo 'Parse error: ' . $e->getMessage() . "\n"; |
238 | 274 | } |
239 | 275 |
|
240 | 276 | return; |
|
0 commit comments