Skip to content

Commit

Permalink
Fix dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
meezaan committed Jul 13, 2022
1 parent 23dbd61 commit a22968e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
24 changes: 5 additions & 19 deletions config/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);


71 changes: 71 additions & 0 deletions src/AlQuranCloud/Handler/AlQuranExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace AlQuranCloud\Handler;

use Psr\Http\Message\ResponseInterface;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpException;
use Slim\Exception\HttpForbiddenException;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpNotImplementedException;
use Slim\Exception\HttpUnauthorizedException;
use Slim\Handlers\ErrorHandler;
use Exception;
use Throwable;

class AlQuranExceptionHandler extends ErrorHandler
{
public const BAD_REQUEST = 'BAD_REQUEST';
public const INSUFFICIENT_PRIVILEGES = 'INSUFFICIENT_PRIVILEGES';
public const NOT_ALLOWED = 'NOT_ALLOWED';
public const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const SERVER_ERROR = 'SERVER_ERROR';
public const UNAUTHENTICATED = 'UNAUTHENTICATED';

protected function respond(): ResponseInterface
{
$exception = $this->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');

}
}

0 comments on commit a22968e

Please sign in to comment.