Skip to content

Commit

Permalink
Introduce ErrorHandlingMode
Browse files Browse the repository at this point in the history
  • Loading branch information
peldax committed Nov 12, 2023
1 parent 6093352 commit c7400f1
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/Graphpinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ 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;
private \Graphpinator\Resolver\Resolver $resolver;

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();
Expand Down Expand Up @@ -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),
};
}
}

Expand All @@ -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()) {
Expand Down

0 comments on commit c7400f1

Please sign in to comment.