*/ class ExceptionInterpretor implements ServiceLocatorAwareInterface { use ServiceLocatorAwareTrait; /** * * @var Exception */ protected $exception; /** * * @var integer */ protected $returnHttpCode; /** * @var string[]|null */ protected $allowedRequestMethods; /** * * @var string */ protected $responseClassName; /** * * @var string */ protected $trace = ''; /** * set exception to interpet * @param Exception $exception * @return ExceptionInterpretor */ public function setException(Exception $exception) { $this->exception = $exception; $this->interpretError(); return $this; } /** * interpret exception type and set up render responseClassName * and http status to return */ protected function interpretError() { switch (get_class($this->exception)) { case UserErrorException::class: case tao_models_classes_MissingRequestParameterException::class: case common_exception_MissingParameter::class: case common_exception_BadRequest::class: $this->returnHttpCode = StatusCode::HTTP_BAD_REQUEST; $this->responseClassName = 'MainResponse'; break; case 'tao_models_classes_AccessDeniedException': case 'ResolverException': $this->returnHttpCode = StatusCode::HTTP_FORBIDDEN; $this->responseClassName = 'RedirectResponse'; break; case 'tao_models_classes_UserException': $this->returnHttpCode = StatusCode::HTTP_FORBIDDEN; $this->responseClassName = 'MainResponse'; break; case 'ActionEnforcingException': case 'tao_models_classes_FileNotFoundException': case common_exception_ResourceNotFound::class: $this->returnHttpCode = StatusCode::HTTP_NOT_FOUND; $this->responseClassName = 'MainResponse'; break; case common_exception_MethodNotAllowed::class: $this->returnHttpCode = StatusCode::HTTP_METHOD_NOT_ALLOWED; $this->responseClassName = 'MainResponse'; /** @var common_exception_MethodNotAllowed $exception */ $exception = $this->exception; $this->allowedRequestMethods = $exception->getAllowedMethods(); break; default: $this->responseClassName = 'MainResponse'; $this->returnHttpCode = StatusCode::HTTP_INTERNAL_SERVER_ERROR; break; } return $this; } public function getTrace() { return $this->exception ? $this->exception->getMessage() : ''; } /** * @return integer */ public function getHttpCode() { return $this->returnHttpCode; } /** * return string */ public function getResponseClassName() { return __NAMESPACE__ . '\\' . $this->responseClassName; } /** * return an instance of ResponseInterface * * @return ResponseAbstract */ public function getResponse() { $class = $this->getResponseClassName(); /** @var $response ResponseAbstract */ $response = new $class(); $response->setServiceLocator($this->getServiceLocator()); $response->setException($this->exception) ->setHttpCode($this->returnHttpCode) ->setAllowedMethods($this->allowedRequestMethods) ->trace(); return $response; } }