mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-02 19:58:22 +08:00
Added default exception handler to handle exception.
This commit is contained in:
parent
d9896a439d
commit
7188784cb1
@ -85,9 +85,7 @@ class Server implements OnRequestInterface, MiddlewareInitializerInterface
|
||||
|
||||
$config = $this->container->get(ConfigInterface::class);
|
||||
$this->middlewares = $config->get('middlewares.' . $serverName, []);
|
||||
$this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, [
|
||||
HttpExceptionHandler::class,
|
||||
]);
|
||||
$this->exceptionHandlers = $config->get('exceptions.handler.' . $serverName, $this->getDefaultExceptionHandler());
|
||||
}
|
||||
|
||||
public function onRequest(SwooleRequest $request, SwooleResponse $response): void
|
||||
@ -125,6 +123,13 @@ class Server implements OnRequestInterface, MiddlewareInitializerInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getDefaultExceptionHandler(): array
|
||||
{
|
||||
return [
|
||||
HttpExceptionHandler::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function createCoreMiddleware(): MiddlewareInterface
|
||||
{
|
||||
$coreHandler = $this->coreHandler;
|
||||
|
@ -48,8 +48,11 @@ class CoreMiddleware extends \Hyperf\RpcServer\CoreMiddleware
|
||||
$parameters = $this->parseParameters($controller, $action, $request->getParsedBody());
|
||||
try {
|
||||
$response = $controllerInstance->{$action}(...$parameters);
|
||||
} catch (\Exception $e) {
|
||||
return $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::SERVER_ERROR, $e);
|
||||
} catch (\Exception $exception) {
|
||||
$response = $this->responseBuilder->buildErrorResponse($request, ResponseBuilder::SERVER_ERROR, $exception);
|
||||
$this->responseBuilder->saveResponse($response);
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
return $response;
|
||||
|
47
src/json-rpc/src/Exception/Handler/HttpExceptionHandler.php
Normal file
47
src/json-rpc/src/Exception/Handler/HttpExceptionHandler.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://doc.hyperf.io
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
namespace Hyperf\JsonRpc\Exception\Handler;
|
||||
|
||||
use Exception;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\ExceptionHandler;
|
||||
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Throwable;
|
||||
|
||||
class HttpExceptionHandler extends ExceptionHandler
|
||||
{
|
||||
protected $logger;
|
||||
|
||||
protected $formatter;
|
||||
|
||||
public function __construct(StdoutLoggerInterface $logger, FormatterInterface $formatter)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->formatter = $formatter;
|
||||
}
|
||||
|
||||
public function handle(Throwable $throwable, ResponseInterface $response)
|
||||
{
|
||||
$this->logger->warning($this->formatter->format($throwable));
|
||||
|
||||
$this->stopPropagation();
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function isValid(Throwable $throwable): bool
|
||||
{
|
||||
return $throwable instanceof Exception;
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ namespace Hyperf\JsonRpc;
|
||||
use Hyperf\HttpMessage\Server\Request as Psr7Request;
|
||||
use Hyperf\HttpMessage\Server\Response as Psr7Response;
|
||||
use Hyperf\HttpServer\Server;
|
||||
use Hyperf\JsonRpc\Exception\Handler\HttpExceptionHandler;
|
||||
use Hyperf\Rpc\Protocol;
|
||||
use Hyperf\Rpc\ProtocolManager;
|
||||
use Hyperf\Utils\Context;
|
||||
@ -59,6 +60,13 @@ class HttpServer extends Server
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getDefaultExceptionHandler(): array
|
||||
{
|
||||
return [
|
||||
HttpExceptionHandler::class,
|
||||
];
|
||||
}
|
||||
|
||||
protected function createCoreMiddleware(): MiddlewareInterface
|
||||
{
|
||||
$coreHandler = $this->coreHandler;
|
||||
|
@ -63,6 +63,11 @@ class ResponseBuilder
|
||||
->withBody($body);
|
||||
}
|
||||
|
||||
public function saveResponse(ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
return Context::set(ResponseInterface::class, $response);
|
||||
}
|
||||
|
||||
protected function formatResponse($response, ServerRequestInterface $request): string
|
||||
{
|
||||
$response = $this->dataFormatter->formatResponse([$request->getAttribute('request_id') ?? '', $response]);
|
||||
|
Loading…
Reference in New Issue
Block a user