From c7400f1d76e40bb8e472e14eb83c8e0dddf3d894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Pel=C3=AD=C5=A1ek?= Date: Sun, 12 Nov 2023 20:00:44 +0100 Subject: [PATCH] Introduce ErrorHandlingMode --- src/Graphpinator.php | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Graphpinator.php b/src/Graphpinator.php index d27920b17..5b7065bd3 100644 --- a/src/Graphpinator.php +++ b/src/Graphpinator.php @@ -10,6 +10,7 @@ final class Graphpinator implements \Psr\Log\LoggerAwareInterface * Whether Graphpinator should perform schema integrity checks. Disable in production to avoid unnecessary overhead. */ public static bool $validateSchema = true; + private ErrorHandlingMode $errorHandlingMode; private \Graphpinator\Parser\Parser $parser; private \Graphpinator\Normalizer\Normalizer $normalizer; private \Graphpinator\Normalizer\Finalizer $finalizer; @@ -17,11 +18,14 @@ final class Graphpinator implements \Psr\Log\LoggerAwareInterface public function __construct( \Graphpinator\Typesystem\Schema $schema, - private bool $catchExceptions = false, + bool|ErrorHandlingMode $errorHandlingMode = ErrorHandlingMode::NONE, private \Graphpinator\Module\ModuleSet $modules = new \Graphpinator\Module\ModuleSet([]), private \Psr\Log\LoggerInterface $logger = new \Psr\Log\NullLogger(), ) { + $this->errorHandlingMode = $errorHandlingMode instanceof ErrorHandlingMode + ? $errorHandlingMode + : ErrorHandlingMode::fromBool($errorHandlingMode); $this->parser = new \Graphpinator\Parser\Parser(); $this->normalizer = new \Graphpinator\Normalizer\Normalizer($schema); $this->finalizer = new \Graphpinator\Normalizer\Finalizer(); @@ -84,17 +88,13 @@ public function run(\Graphpinator\Request\RequestFactory $requestFactory) : \Gra return $result; } catch (\Throwable $exception) { - if (!$this->catchExceptions) { - throw $exception; - } - $this->logger->log(self::getLogLevel($exception), self::getLogMessage($exception)); - return new \Graphpinator\Result(null, [ - $exception instanceof \Graphpinator\Exception\ClientAware - ? self::serializeError($exception) - : self::notOutputableResponse(), - ]); + return match($this->errorHandlingMode) { + ErrorHandlingMode::ALL => $this->handleAll($exception), + ErrorHandlingMode::CLIENT_AWARE => $this->handleClientAware($exception), + ErrorHandlingMode::NONE => $this->handleNone($exception), + }; } } @@ -119,6 +119,27 @@ private static function getLogLevel(\Throwable $exception) : string return \Psr\Log\LogLevel::EMERGENCY; } + private function handleAll(\Throwable $exception) : Result + { + return new \Graphpinator\Result(null, [ + $exception instanceof \Graphpinator\Exception\ClientAware + ? self::serializeError($exception) + : self::notOutputableResponse(), + ]); + } + + private function handleClientAware(\Throwable $exception) : Result + { + return $exception instanceof \Graphpinator\Exception\ClientAware + ? new \Graphpinator\Result(null, [self::serializeError($exception)]) + : throw $exception; + } + + private function handleNone(\Throwable $exception) : never + { + throw $exception; + } + private static function serializeError(\Graphpinator\Exception\ClientAware $exception) : array { if (!$exception->isOutputable()) {