/** * @param ProtocolException $exception * * @return E\ErrorInterface */ private function getErrorForOxygenProtocolException(ProtocolException $exception) { if ($exception instanceof NetworkException) { // There was a network error; we did not get a full response. switch ($exception->getOriginalCode()) { case CURLE_COULDNT_RESOLVE_HOST: // Hostname lookup failed. return new E\Network\CanNotResolveHost(); case CURLE_COULDNT_CONNECT: return new E\Network\CanNotConnect(); case CURLE_OPERATION_TIMEOUTED: return new E\Network\TimedOut($exception->getTransferInfo()->total_time); case CURLE_SEND_ERROR: return new E\Network\SendError(); case CURLE_RECV_ERROR: return new E\Network\ReceiveError(); default: return new E\Api\UnexpectedError(); } } elseif ($exception instanceof ResponseException) { // We got a full response, but did not find the Oxygen module's response. if ($exception->getResponse()->hasHeader('www-authenticate') && $exception->getResponse()->getStatusCode() === 401) { // HTTP authorization encountered. $realm = ''; preg_match('{^Basic realm="(.*)"$}i', $exception->getResponse()->getHeaderLine('www-authenticate'), $matches); if (!empty($matches[1])) { $realm = $matches[1]; } return new E\Response\Unauthorized($realm, $exception->getRequest()->hasHeader('authorize')); } else { // @todo: Add special handling for >=400 status codes. return new E\Response\OxygenNotFound(); } } elseif ($exception instanceof OxygenException) { // We got an exception or a fatal error directly from the Oxygen module. $exceptionData = $exception->getExceptionData(); // This is a fatal error or an exception. return new E\Oxygen\Error($exceptionData->getMessage(), $exception->getCode(), $exceptionData->getType(), $exceptionData->getFile(), $exceptionData->getLine(), $exceptionData->getTraceString()); } else { // This should never ever happen. return new E\Api\UnexpectedError(); } }