| @@ 22-57 (lines=36) @@ | ||
| 19 | * Class GenericRouterGenericServerHTTPServer | |
| 20 | * @package LunixREST | |
| 21 | */ | |
| 22 | class GenericRouterGenericServerHTTPServer extends GenericServerHTTPServer | |
| 23 | { | |
| 24 | public function __construct(GenericRouterGenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger) | |
| 25 |     { | |
| 26 | parent::__construct($server, $requestFactory, $logger); | |
| 27 | } | |
| 28 | ||
| 29 | protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface | |
| 30 |     { | |
| 31 |         if($exception instanceof InvalidRequestException) { | |
| 32 | $this->logCaughtThrowableResultingInHTTPCode(400, $exception, LogLevel::INFO); | |
| 33 | return $response->withStatus(400, "Bad Request"); | |
| 34 | ||
| 35 | } elseif( | |
| 36 | $exception instanceof ElementNotFoundException || | |
| 37 | $exception instanceof UnsupportedMethodException || | |
| 38 | $exception instanceof UnableToCreateEndpointException | |
| 39 |         ) { | |
| 40 | $this->logCaughtThrowableResultingInHTTPCode(404, $exception, LogLevel::INFO); | |
| 41 | return $response->withStatus(404, "Not Found"); | |
| 42 | ||
| 43 |         } elseif($exception instanceof ElementConflictException) { | |
| 44 | $this->logCaughtThrowableResultingInHTTPCode(409, $exception, LogLevel::NOTICE); | |
| 45 | return $response->withStatus(409, "Conflict"); | |
| 46 | ||
| 47 | } elseif( | |
| 48 | $exception instanceof MethodNotFoundException || | |
| 49 | $exception instanceof EndpointExecutionException | |
| 50 |         ) { | |
| 51 | $this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL); | |
| 52 | return $response->withStatus(500, "Internal Server Error"); | |
| 53 | } | |
| 54 | ||
| 55 | return parent::handleServerException($exception, $response); | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| @@ 22-64 (lines=43) @@ | ||
| 19 | * Class GenericServerHTTPServer | |
| 20 | * @package LunixREST | |
| 21 | */ | |
| 22 | class GenericServerHTTPServer extends HTTPServer | |
| 23 | { | |
| 24 | /** | |
| 25 | * GenericServerHTTPServer constructor. | |
| 26 | * @param GenericServer $server | |
| 27 | * @param RequestFactory $requestFactory | |
| 28 | * @param LoggerInterface $logger | |
| 29 | */ | |
| 30 | public function __construct(GenericServer $server, RequestFactory $requestFactory, LoggerInterface $logger) | |
| 31 |     { | |
| 32 | parent::__construct($server, $requestFactory, $logger); | |
| 33 | } | |
| 34 | ||
| 35 | /** | |
| 36 | * @param UnableToHandleRequestException $exception | |
| 37 | * @param ResponseInterface $response | |
| 38 | * @return ResponseInterface | |
| 39 | */ | |
| 40 | protected function handleServerException(UnableToHandleRequestException $exception, ResponseInterface $response): ResponseInterface | |
| 41 |     { | |
| 42 |         if($exception instanceof InvalidAPIKeyException || $exception instanceof AccessDeniedException) { | |
| 43 | $this->logCaughtThrowableResultingInHTTPCode(403, $exception, LogLevel::NOTICE); | |
| 44 | return $response->withStatus(403, "Access Denied"); | |
| 45 | ||
| 46 |         } elseif($exception instanceof ThrottleLimitExceededException) { | |
| 47 | $this->logCaughtThrowableResultingInHTTPCode(429, $exception, LogLevel::WARNING); | |
| 48 | return $response->withStatus(429, "Too Many Requests"); | |
| 49 | ||
| 50 |         } elseif($exception instanceof NotAcceptableResponseTypeException) { | |
| 51 | $this->logCaughtThrowableResultingInHTTPCode(406, $exception, LogLevel::INFO); | |
| 52 | return $response->withStatus(406, "Not Acceptable"); | |
| 53 | ||
| 54 | } elseif( | |
| 55 | $exception instanceof UnableToCreateAPIResponseException || | |
| 56 | $exception instanceof UnableToRouteRequestException | |
| 57 |         ) { | |
| 58 | $this->logCaughtThrowableResultingInHTTPCode(500, $exception, LogLevel::CRITICAL); | |
| 59 | return $response->withStatus(500, "Internal Server Error"); | |
| 60 | } | |
| 61 | ||
| 62 | return parent::handleServerException($exception, $response); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | ||