|
8 | 8 | use Tempest\Console\ExitCode; |
9 | 9 | use Tempest\Console\HasExitCode; |
10 | 10 | use Tempest\Console\Input\ConsoleArgumentBag; |
| 11 | +use Tempest\Container\Container; |
11 | 12 | use Tempest\Container\Tag; |
12 | | -use Tempest\Core\ErrorHandler; |
| 13 | +use Tempest\Core\AppConfig; |
| 14 | +use Tempest\Core\ExceptionHandler; |
13 | 15 | use Tempest\Core\Kernel; |
14 | 16 | use Tempest\Highlight\Escape; |
15 | 17 | use Tempest\Highlight\Highlighter; |
16 | 18 | use Throwable; |
17 | 19 |
|
18 | 20 | use function Tempest\Support\str; |
19 | 21 |
|
20 | | -final readonly class ConsoleErrorHandler implements ErrorHandler |
| 22 | +final readonly class ConsoleExceptionHandler implements ExceptionHandler |
21 | 23 | { |
22 | 24 | public function __construct( |
| 25 | + private AppConfig $appConfig, |
| 26 | + private Container $container, |
23 | 27 | private Kernel $kernel, |
24 | 28 | #[Tag('console')] |
25 | 29 | private Highlighter $highlighter, |
26 | 30 | private Console $console, |
27 | 31 | private ConsoleArgumentBag $argumentBag, |
28 | 32 | ) {} |
29 | 33 |
|
30 | | - public function handleException(Throwable $throwable): void |
| 34 | + public function handle(Throwable $throwable): void |
31 | 35 | { |
32 | | - ll(exception: $throwable->getMessage()); |
33 | | - |
34 | | - $this->console |
35 | | - ->writeln() |
36 | | - ->error($throwable::class) |
37 | | - ->when( |
38 | | - condition: $throwable->getMessage(), |
39 | | - callback: fn (Console $console) => $console->error($throwable->getMessage()), |
40 | | - ) |
41 | | - ->writeln() |
42 | | - ->writeln('In ' . $this->formatFileWithLine($throwable->getFile() . ':' . $throwable->getLine())) |
43 | | - ->writeln($this->getSnippet($throwable->getFile(), $throwable->getLine())) |
44 | | - ->writeln(); |
45 | | - |
46 | | - if ($this->argumentBag->get('-v') !== null) { |
47 | | - foreach ($throwable->getTrace() as $i => $trace) { |
48 | | - $this->console->writeln("<style='bold fg-blue'>#{$i}</style> " . $this->formatTrace($trace)); |
| 36 | + try { |
| 37 | + foreach ($this->appConfig->exceptionProcessors as $processor) { |
| 38 | + $handler = $this->container->get($processor); |
| 39 | + $throwable = $handler->process($throwable); |
49 | 40 | } |
50 | 41 |
|
51 | | - $this->console->writeln(); |
52 | | - } else { |
53 | 42 | $this->console |
54 | | - ->writeln('<style="fg-blue bold">#0</style> ' . $this->formatTrace($throwable->getTrace()[0])) |
55 | | - ->writeln('<style="fg-blue bold">#1</style> ' . $this->formatTrace($throwable->getTrace()[1])) |
56 | 43 | ->writeln() |
57 | | - ->writeln(' <style="dim">Run with -v to show more.</style>') |
| 44 | + ->error($throwable::class) |
| 45 | + ->when( |
| 46 | + condition: $throwable->getMessage(), |
| 47 | + callback: fn (Console $console) => $console->error($throwable->getMessage()), |
| 48 | + ) |
| 49 | + ->writeln() |
| 50 | + ->writeln('In ' . $this->formatFileWithLine($throwable->getFile() . ':' . $throwable->getLine())) |
| 51 | + ->writeln($this->getSnippet($throwable->getFile(), $throwable->getLine())) |
58 | 52 | ->writeln(); |
59 | | - } |
60 | | - |
61 | | - $exitCode = ($throwable instanceof HasExitCode) ? $throwable->getExitCode() : ExitCode::ERROR; |
62 | | - |
63 | | - $this->kernel->shutdown($exitCode->value); |
64 | | - } |
65 | 53 |
|
66 | | - public function handleError(int $errNo, string $errstr, string $errFile, int $errLine): void |
67 | | - { |
68 | | - ll(error: $errstr); |
| 54 | + if ($this->argumentBag->get('-v') !== null) { |
| 55 | + foreach ($throwable->getTrace() as $i => $trace) { |
| 56 | + $this->console->writeln("<style='bold fg-blue'>#{$i}</style> " . $this->formatTrace($trace)); |
| 57 | + } |
| 58 | + |
| 59 | + $this->console->writeln(); |
| 60 | + } else { |
| 61 | + $this->console |
| 62 | + ->writeln('<style="fg-blue bold">#0</style> ' . $this->formatTrace($throwable->getTrace()[0])) |
| 63 | + ->writeln('<style="fg-blue bold">#1</style> ' . $this->formatTrace($throwable->getTrace()[1])) |
| 64 | + ->writeln() |
| 65 | + ->writeln(' <style="dim">Run with -v to show more.</style>') |
| 66 | + ->writeln(); |
| 67 | + } |
| 68 | + } finally { |
| 69 | + $exitCode = ($throwable instanceof HasExitCode) |
| 70 | + ? $throwable->getExitCode() |
| 71 | + : ExitCode::ERROR; |
69 | 72 |
|
70 | | - $this->console |
71 | | - ->writeln() |
72 | | - ->error($errstr) |
73 | | - ->writeln($this->getSnippet($errFile, $errLine)); |
| 73 | + $this->kernel->shutdown($exitCode->value); |
| 74 | + } |
74 | 75 | } |
75 | 76 |
|
76 | 77 | private function getSnippet(string $file, int $lineNumber): string |
|
0 commit comments