From a22968ea2f63c0c9c897aca4a0c78dd7cc0f69d4 Mon Sep 17 00:00:00 2001 From: Meezaan-ud-Din Abdu Dhil-Jalali Wal-Ikram Date: Wed, 13 Jul 2022 19:12:49 +0400 Subject: [PATCH] Fix dependencies --- config/dependencies.php | 24 ++----- .../Handler/AlQuranExceptionHandler.php | 71 +++++++++++++++++++ 2 files changed, 76 insertions(+), 19 deletions(-) create mode 100644 src/AlQuranCloud/Handler/AlQuranExceptionHandler.php diff --git a/config/dependencies.php b/config/dependencies.php index 47e020c..cfe9775 100644 --- a/config/dependencies.php +++ b/config/dependencies.php @@ -38,25 +38,11 @@ }); // Application middleware -$errorMiddleware = $app->addErrorMiddleware(true, true, true); - -$errorMiddleware->setErrorHandler( - Slim\Exception\HttpNotFoundException::class, - function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) use ($container) { - $response = new Response(); - $response->getBody()->write('Sorry, we could not find the URL you are after.'); - return $response->withStatus(404); - }); - -$errorMiddleware->setErrorHandler( - HttpMethodNotAllowedException::class, - function (ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails) use ($container) { - $logger = $containter->get('logger'); - $logger->error('Slim Error Handler Triggered', ['code' => $exception->getCode(), 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()]); - $response = new Response(); - $response->getBody()->write($exception->getMessage()); - return $response->withStatus($exception->getCode()); - }); +$errorMiddleware = $app->addErrorMiddleware(false, true, true); +$callableResolver = $app->getCallableResolver(); +$responseFactory = $app->getResponseFactory(); +$errorHandler = new \AlQuranCloud\Handler\AlQuranExceptionHandler($callableResolver, $responseFactory); +$errorMiddleware->setDefaultErrorHandler($errorHandler); diff --git a/src/AlQuranCloud/Handler/AlQuranExceptionHandler.php b/src/AlQuranCloud/Handler/AlQuranExceptionHandler.php new file mode 100644 index 0000000..330b82c --- /dev/null +++ b/src/AlQuranCloud/Handler/AlQuranExceptionHandler.php @@ -0,0 +1,71 @@ +exception; + $statusCode = 500; + $type = self::SERVER_ERROR; + + if ($exception instanceof HttpException) { + if ($statusCode >= 200) { + $statusCode = $exception->getCode(); + } + + + if ($exception instanceof HttpNotFoundException) { + $type = self::RESOURCE_NOT_FOUND; + } elseif ($exception instanceof HttpMethodNotAllowedException) { + $type = self::NOT_ALLOWED; + } elseif ($exception instanceof HttpUnauthorizedException) { + $type = self::UNAUTHENTICATED; + } elseif ($exception instanceof HttpForbiddenException) { + $type = self::UNAUTHENTICATED; + } elseif ($exception instanceof HttpBadRequestException) { + $type = self::BAD_REQUEST; + } elseif ($exception instanceof HttpNotImplementedException) { + $type = self::NOT_IMPLEMENTED; + } + } + + $description = $exception->getMessage(); + + $error = [ + 'code' => $statusCode, + 'status' => $type, + 'message' => $description + ]; + + $payload = json_encode($error, JSON_PRETTY_PRINT); + + $response = $this->responseFactory->createResponse($statusCode); + + $response->getBody()->write($payload); + + return $response->withHeader('Content-Type', 'application/json'); + + } +} \ No newline at end of file