Skip to content

Commit

Permalink
Catch await exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmorbo committed Oct 12, 2023
1 parent 3b9eddb commit 693cb7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
20 changes: 20 additions & 0 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@

namespace AlexMorbo\React\Trassir\Controller;

use HttpSoft\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use React\Promise\PromiseInterface;
use Throwable;
use Tnapf\Router\Router;

use function React\Async\await;

abstract class AbstractController
{
protected ?LoggerInterface $logger = null;

public function addRoutes(Router $router): void
{
}

protected function awaitResponse(PromiseInterface $promise): ResponseInterface
{
try {
return await($promise);
} catch (Throwable $e) {
$this->logger->error('Async error', ['message' => $e->getMessage()]);

return new JsonResponse(['status' => 'error', 'error' => $e->getMessage()], 404);
}
}
}
16 changes: 7 additions & 9 deletions src/Controller/InstanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace AlexMorbo\React\Trassir\Controller;

use AlexMorbo\React\Trassir\Dto\Instance;
use AlexMorbo\React\Trassir\Log;
use AlexMorbo\React\Trassir\Traits\DBTrait;
use AlexMorbo\React\Trassir\TrassirHelper;
use AlexMorbo\Trassir\TrassirException;
Expand All @@ -19,7 +18,6 @@
use Tnapf\Router\Router;
use Tnapf\Router\Routing\RouteRunner;

use function React\Async\await;
use function React\Promise\all;
use function React\Promise\resolve;

Expand All @@ -28,7 +26,7 @@ class InstanceController extends AbstractController
use DBTrait;

public function __construct(
private LoggerInterface $logger,
protected ?LoggerInterface $logger,
DatabaseInterface $db,
protected TrassirHelper $trassirHelper
) {
Expand Down Expand Up @@ -132,7 +130,7 @@ function (Instance $instance) use ($instanceData) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}

public function getInstance(
Expand Down Expand Up @@ -161,7 +159,7 @@ function (Instance $instance) use ($result) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}

public function addInstance(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
Expand Down Expand Up @@ -237,7 +235,7 @@ function ($error) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}

public function deleteInstance(
Expand Down Expand Up @@ -271,7 +269,7 @@ function ($error) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}

public function getChannelScreenshot(
Expand Down Expand Up @@ -304,7 +302,7 @@ function ($screenshot) use ($response) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}

public function getChannelVideo(
Expand Down Expand Up @@ -364,7 +362,7 @@ function (Exception $e) {
}
);

return await($promise);
return $this->awaitResponse($promise);
}


Expand Down

0 comments on commit 693cb7c

Please sign in to comment.