Skip to content

Commit b1937cc

Browse files
committed
refactor(music-command): Replace exception handler method
- Replace 'wrappedExceptionHandler' with 'rescue' for better clarity - Update exception handling to use Laravel's built-in rescue function - Improve error reporting in console by utilizing the ExceptionHandler
1 parent a307708 commit b1937cc

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

app/Commands/MusicCommand.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use App\Support\Utils;
2222
use Illuminate\Console\Scheduling\Schedule;
2323
use Illuminate\Contracts\Console\Isolatable;
24+
use Illuminate\Contracts\Debug\ExceptionHandler;
2425
use Illuminate\Support\Collection;
2526
use Illuminate\Support\Facades\File;
2627
use LaravelZero\Framework\Commands\Command;
@@ -29,7 +30,6 @@
2930
use Symfony\Component\Console\Input\InputInterface;
3031
use Symfony\Component\Console\Output\OutputInterface;
3132
use function Laravel\Prompts\confirm;
32-
use function Laravel\Prompts\error;
3333
use function Laravel\Prompts\info;
3434
use function Laravel\Prompts\multiselect;
3535
use function Laravel\Prompts\spin;
@@ -110,11 +110,11 @@ function () use ($keyword, &$duration): Collection {
110110
static fn (Collection $songs): Collection => \in_array(0, $selectedKeys->all(), true)
111111
? $songs : $songs->only($selectedKeys->all())
112112
)
113-
->each(fn (array $song): mixed => $this->wrappedExceptionHandler(fn () => $this->music->download(
113+
->each(fn (array $song): mixed => $this->rescue(fn () => $this->music->download(
114114
$song['url'],
115115
Utils::savedPathFor($song, $this->option('dir'))
116116
)))
117-
->tap(fn (): mixed => $this->wrappedExceptionHandler(fn () => $this->notify(
117+
->tap(fn (): mixed => $this->rescue(fn () => $this->notify(
118118
config('app.name'),
119119
$this->option('dir'),
120120
resource_path('notify-icon.png')
@@ -147,14 +147,26 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
147147
$this->input->setOption('sources', array_filter((array) $this->option('sources')) ?: config('app.sources'));
148148
}
149149

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
151161
{
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);
156166

157-
return $throwable;
158-
}
167+
return $throwable;
168+
},
169+
$report
170+
);
159171
}
160172
}

0 commit comments

Comments
 (0)