Skip to content

Commit

Permalink
add try-catch for dispatch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 13, 2020
1 parent f5b793d commit a7059bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
21 changes: 13 additions & 8 deletions src/http-server/src/HttpDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Swoft\Http\Server\Middleware\DefaultMiddleware;
use Swoft\Http\Server\Middleware\UserMiddleware;
use Swoft\Http\Server\Router\Router;
use Swoft\Log\Error;
use Swoft\Log\Logger;
use Swoft\Server\SwooleEvent;
use Swoft\SwoftEvent;
Expand Down Expand Up @@ -71,9 +72,10 @@ public function dispatch(...$params): void

/* @var RequestHandler $requestHandler */
$requestHandler = Swoft::getBean(RequestHandler::class);
$requestHandler->initialize($this->requestMiddlewares, $this->defaultMiddleware);

try {
$requestHandler->initialize($this->requestMiddlewares, $this->defaultMiddleware);

// Before request
$this->beforeRequest($request, $response);

Expand All @@ -91,14 +93,18 @@ public function dispatch(...$params): void
$response = $errDispatcher->run($e, $response);
}

// Format response content type
$response = $this->acceptFormatter->format($response);
try {
// Format response content type
$response = $this->acceptFormatter->format($response);

// Trigger after request
Swoft::trigger(HttpServerEvent::AFTER_REQUEST, null, $response);
// Trigger after request
Swoft::trigger(HttpServerEvent::AFTER_REQUEST, null, $response);

// After request
$this->afterRequest($response);
// After request
$this->afterRequest($response);
} catch (Throwable $e) {
Error::log('response error=%s(%d) at %s:%d', $e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
}
}

/**
Expand Down Expand Up @@ -154,7 +160,6 @@ private function afterRequest(Response $response): void
*/
private function matchRouter(Request $request): Request
{
/** @var Request $request $method */
$method = $request->getMethod();
$uriPath = $request->getUriPath();

Expand Down
3 changes: 0 additions & 3 deletions src/http-server/src/Swoole/RequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Exception\SwoftException;
use Swoft\Http\Message\Request as ServerRequest;
use Swoft\Http\Message\Response as ServerResponse;
use Swoft\Http\Server\HttpDispatcher;
Expand All @@ -39,8 +38,6 @@ class RequestListener implements RequestInterface
/**
* @param Request $request
* @param Response $response
*
* @throws SwoftException
*/
public function onRequest(Request $request, Response $response): void
{
Expand Down
14 changes: 8 additions & 6 deletions src/process/src/Listener/AddProcessListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ private function addProcesses(Server $server): void
$function = function (SwooleProcess $swProcess) use ($callback, $server, $name) {
$process = Process::new($swProcess);

// Before
Swoft::trigger(ProcessEvent::BEFORE_USER_PROCESS, null, $server, $process, $name);
try {
// Before
Swoft::trigger(ProcessEvent::BEFORE_USER_PROCESS, null, $server, $process, $name);

try {// Run
// Run
// TODO use $userProcess->run($process);
PhpHelper::call($callback, $process);

// After
Swoft::trigger(ProcessEvent::AFTER_USER_PROCESS);
} catch (Throwable $e) {
Error::log('User process fail(%s %s %d)!', $e->getFile(), $e->getMessage(), $e->getLine());
}

// After
Swoft::trigger(ProcessEvent::AFTER_USER_PROCESS);
};

$process = new SwooleProcess($function, $stdinOut, $pipeType, $coroutine);
Expand Down

0 comments on commit a7059bf

Please sign in to comment.