|
21 | 21 | use App\Support\Utils; |
22 | 22 | use Illuminate\Console\Scheduling\Schedule; |
23 | 23 | use Illuminate\Contracts\Console\Isolatable; |
| 24 | +use Illuminate\Contracts\Debug\ExceptionHandler; |
24 | 25 | use Illuminate\Support\Collection; |
25 | 26 | use Illuminate\Support\Facades\File; |
26 | 27 | use LaravelZero\Framework\Commands\Command; |
|
29 | 30 | use Symfony\Component\Console\Input\InputInterface; |
30 | 31 | use Symfony\Component\Console\Output\OutputInterface; |
31 | 32 | use function Laravel\Prompts\confirm; |
32 | | -use function Laravel\Prompts\error; |
33 | 33 | use function Laravel\Prompts\info; |
34 | 34 | use function Laravel\Prompts\multiselect; |
35 | 35 | use function Laravel\Prompts\spin; |
@@ -110,11 +110,11 @@ function () use ($keyword, &$duration): Collection { |
110 | 110 | static fn (Collection $songs): Collection => \in_array(0, $selectedKeys->all(), true) |
111 | 111 | ? $songs : $songs->only($selectedKeys->all()) |
112 | 112 | ) |
113 | | - ->each(fn (array $song): mixed => $this->wrappedExceptionHandler(fn () => $this->music->download( |
| 113 | + ->each(fn (array $song): mixed => $this->rescue(fn () => $this->music->download( |
114 | 114 | $song['url'], |
115 | 115 | Utils::savedPathFor($song, $this->option('dir')) |
116 | 116 | ))) |
117 | | - ->tap(fn (): mixed => $this->wrappedExceptionHandler(fn () => $this->notify( |
| 117 | + ->tap(fn (): mixed => $this->rescue(fn () => $this->notify( |
118 | 118 | config('app.name'), |
119 | 119 | $this->option('dir'), |
120 | 120 | resource_path('notify-icon.png') |
@@ -147,14 +147,26 @@ protected function initialize(InputInterface $input, OutputInterface $output): v |
147 | 147 | $this->input->setOption('sources', array_filter((array) $this->option('sources')) ?: config('app.sources')); |
148 | 148 | } |
149 | 149 |
|
150 | | - private function wrappedExceptionHandler(callable $callback, mixed ...$parameters): mixed |
| 150 | + /** |
| 151 | + * @template TValue |
| 152 | + * |
| 153 | + * @param callable(): TValue $callback |
| 154 | + * @param bool|callable(\Throwable): bool $report |
| 155 | + * |
| 156 | + * @throws \Illuminate\Contracts\Container\BindingResolutionException |
| 157 | + * |
| 158 | + * @return \Throwable|TValue |
| 159 | + */ |
| 160 | + private function rescue(callable $callback, bool|callable $report = false): mixed |
151 | 161 | { |
152 | | - try { |
153 | | - return $callback(...$parameters); |
154 | | - } catch (\Throwable $throwable) { |
155 | | - error($throwable->getMessage()); |
| 162 | + return rescue( |
| 163 | + $callback, |
| 164 | + function (\Throwable $throwable): \Throwable { |
| 165 | + $this->laravel->make(ExceptionHandler::class)->renderForConsole($this->output, $throwable); |
156 | 166 |
|
157 | | - return $throwable; |
158 | | - } |
| 167 | + return $throwable; |
| 168 | + }, |
| 169 | + $report |
| 170 | + ); |
159 | 171 | } |
160 | 172 | } |
0 commit comments