diff --git a/Handler/OnRequest.php b/Handler/OnRequest.php index fbb16c3..28649ae 100644 --- a/Handler/OnRequest.php +++ b/Handler/OnRequest.php @@ -67,7 +67,7 @@ class OnRequest implements OnRequestInterface public DataGrip $dataGrip) { $this->responseEmitter = $this->response->emmit; - $exception = \config('request.exception'); + $exception = \config('exception.http'); if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { $exception = ExceptionHandlerDispatcher::class; } diff --git a/Task/Task.php b/Task/Task.php index dd1c97f..ef06186 100644 --- a/Task/Task.php +++ b/Task/Task.php @@ -4,7 +4,14 @@ namespace Kiri\Server\Task; use Kiri; +use Kiri\Router\Base\ExceptionHandlerDispatcher; +use Kiri\Router\DataGrip; +use Kiri\Router\Interface\ExceptionHandlerInterface; use Kiri\Server\Constant; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; +use Psr\Http\Message\ResponseInterface; use Swoole\Server; /** @@ -13,6 +20,25 @@ use Swoole\Server; class Task { + + public ExceptionHandlerInterface $exception; + + + /** + * @param ContainerInterface $container + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function __construct(public ContainerInterface $container) + { + $exception = \config('exception.task'); + if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { + $exception = ExceptionHandlerDispatcher::class; + } + $this->exception = $this->container->get($exception); + } + + /** * @param Server $server * @return void @@ -53,12 +79,16 @@ class Task */ public function onTask(Server $server, int $task_id, int $src_worker_id, mixed $data): mixed { - $data = json_decode($data, true); - if (is_null($data)) { - return null; + try { + $data = json_decode($data, true); + if (is_null($data)) { + return null; + } + $data[0] = Kiri::getDi()->get($data[0]); + return call_user_func($data, $task_id, $src_worker_id); + } catch (\Throwable $throwable) { + return $this->exception->emit($throwable, response()); } - $data[0] = Kiri::getDi()->get($data[0]); - return call_user_func($data, $task_id, $src_worker_id); }