Skip to content

Commit

Permalink
refactor(commands): add Rescuer trait to MusicCommand
Browse files Browse the repository at this point in the history
- Introduced the Rescuer trait to handle exceptions in MusicCommand.
- Removed the private rescue method from MusicCommand to improve code organization.
- Enhanced error handling by utilizing the new Rescuer trait.
  • Loading branch information
guanguans committed Oct 10, 2024
1 parent 0b9f613 commit 7ca0eaf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
29 changes: 2 additions & 27 deletions app/Commands/MusicCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
namespace App\Commands;

use App\Concerns\Hydrator;
use App\Concerns\Rescuer;
use App\Contracts\Music as MusicContract;
use App\Facades\Music;
use App\Support\Utils;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Console\Isolatable;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use LaravelZero\Framework\Commands\Command;
Expand All @@ -40,6 +40,7 @@
final class MusicCommand extends Command implements Isolatable
{
use Hydrator;
use Rescuer;
protected $signature = <<<'SIGNATURE'
music
{keyword? : Search keyword for music}
Expand Down Expand Up @@ -146,30 +147,4 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
File::ensureDirectoryExists($this->option('dir'));
$this->input->setOption('sources', array_filter((array) $this->option('sources')) ?: config('app.sources'));
}

/**
* @psalm-suppress InvalidReturnType
* @psalm-suppress InvalidReturnStatement
*
* @template TValue
*
* @param callable(): TValue $callback
* @param bool|callable(\Throwable): bool $report
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return \Throwable|TValue
*/
private function rescue(callable $callback, bool|callable $report = false): mixed
{
return rescue(
$callback,
function (\Throwable $throwable): \Throwable {
$this->laravel->make(ExceptionHandler::class)->renderForConsole($this->output, $throwable);

return $throwable;
},
$report
);
}
}
50 changes: 50 additions & 0 deletions app/Concerns/Rescuer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2019-2024 guanguans<[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/guanguans/music-dl
*/

namespace App\Concerns;

use Illuminate\Contracts\Debug\ExceptionHandler;

/**
* @mixin \Illuminate\Console\Command
*/
trait Rescuer
{
/**
* @template TValue
*
* @noinspection RedundantDocCommentTagInspection
*
* @psalm-suppress InvalidReturnType
* @psalm-suppress InvalidReturnStatement
*
* @param callable(): TValue $callback
* @param bool|callable(\Throwable): bool $report
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*
* @return \Throwable|TValue
*/
public function rescue(callable $callback, bool|callable $report = false): mixed
{
return rescue(
$callback,
function (\Throwable $throwable): \Throwable {
$this->laravel->make(ExceptionHandler::class)->renderForConsole($this->output, $throwable);

return $throwable;
},
$report
);
}
}

0 comments on commit 7ca0eaf

Please sign in to comment.