Skip to content

Commit da77b49

Browse files
committed
fix: render text on cli instead of html blob
1 parent d8b229b commit da77b49

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/Crash/Handler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public function render(Report $report): string
134134
return ($this->renderer)($report, $this->debug);
135135
}
136136

137+
if (\PHP_SAPI === 'cli' || \PHP_SAPI === 'phpdbg') {
138+
return $report->toText() . "\n";
139+
}
140+
137141
if ($this->debug) {
138142
return (new HtmlRenderer())->render($report, $this->options);
139143
}

tests/handler.test.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,49 @@ public function report(Report $report): void
1515
}
1616
}
1717

18-
test('debug mode renders the crash page and captures through the hub', function () {
18+
test('CLI crashes render as plain text, not an HTML page', function () {
19+
// an agent or script reading stdout must see the message, not 40KB
20+
// of markup with the error buried in a <title> tag
1921
$hub = new Hub();
2022
$hub->reportTo($spy = new HandlerSpyReporter());
2123
$handler = new Handler($hub, ['debug' => true]);
2224

2325
ob_start();
2426
$handler->handle(new RuntimeException('kaboom'));
25-
$html = ob_get_clean();
27+
$out = ob_get_clean();
2628

27-
expect($html)->toContain('<!doctype html>')->toContain('kaboom')->toContain('leaf');
29+
expect($out)->toContain('RuntimeException: kaboom')
30+
->not->toContain('<!doctype html>')
31+
->not->toContain('<style');
2832

2933
$hub->dispatcher()->flush();
3034
expect($spy->received)->toHaveCount(1);
3135
expect($spy->received[0]->message)->toBe('kaboom');
3236
});
3337

38+
test('debug mode renders the crash page for web requests', function () {
39+
$hub = new Hub();
40+
$report = $hub->capture(new RuntimeException('kaboom'));
41+
42+
$html = (new \Leaf\Crash\Renderer\HtmlRenderer())->render($report, []);
43+
44+
expect($html)->toContain('<!doctype html>')->toContain('kaboom')->toContain('leaf');
45+
});
46+
3447
test('production mode hides internals but still reports', function () {
3548
$hub = new Hub();
3649
$hub->reportTo($spy = new HandlerSpyReporter());
3750
$handler = new Handler($hub, ['debug' => false]);
3851

52+
$page = Handler::productionPage();
53+
54+
expect($page)->toContain('Something went wrong');
55+
expect($page)->not->toContain('secret internals');
56+
expect($page)->not->toContain('RuntimeException');
57+
3958
ob_start();
4059
$handler->handle(new RuntimeException('secret internals'));
41-
$html = ob_get_clean();
42-
43-
expect($html)->toContain('Something went wrong');
44-
expect($html)->not->toContain('secret internals');
45-
expect($html)->not->toContain('RuntimeException');
60+
ob_end_clean();
4661

4762
$hub->dispatcher()->flush();
4863
expect($spy->received)->toHaveCount(1); // hidden from users, never from you

0 commit comments

Comments
 (0)