Skip to content

Commit

Permalink
allow flexible exception handling (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxyw authored Dec 27, 2022
1 parent a49f66d commit d394d29
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/OneBot/Exception/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ class ExceptionHandler implements ExceptionHandlerInterface

protected function __construct()
{
$whoops_class = 'Whoops\Run';
$collision_class = 'NunoMaduro\Collision\Handler';
if (class_exists($collision_class) && class_exists($whoops_class)) {
/* @phpstan-ignore-next-line */
$this->whoops = new $whoops_class();
$this->whoops->allowQuit(false);
$this->whoops->writeToOutput(false);
$this->whoops->pushHandler(new $collision_class());
$this->whoops->register();
}
$this->tryEnableCollision();
}

public function getWhoops()
Expand Down Expand Up @@ -64,4 +55,28 @@ protected function handle0(\Throwable $e): void

$this->whoops->handleException($e);
}

protected function tryEnableCollision($solution_repo = null): void
{
$whoops_class = 'Whoops\Run';
$collision_namespace = 'NunoMaduro\Collision';
$collision_handler = "{$collision_namespace}\\Handler";
$collision_writer = "{$collision_namespace}\\Writer";
$collision_repo = "{$collision_namespace}\\Contracts\\SolutionsRepository";
if (class_exists($collision_handler) && class_exists($whoops_class)) {
if ($solution_repo instanceof $collision_repo) {
// @phpstan-ignore-next-line
$writer = new $collision_writer($solution_repo);
} else {
// @phpstan-ignore-next-line
$writer = new $collision_writer();
}

$this->whoops = new $whoops_class();
$this->whoops->allowQuit(false);
$this->whoops->writeToOutput(false);
$this->whoops->pushHandler(new $collision_handler($writer));
$this->whoops->register();
}
}
}

0 comments on commit d394d29

Please sign in to comment.