/** * A factory for creating the appropriate exception based on the response from Wit. * * @param WitResponse $response The response that threw the exception. * * @return WitResponseException */ public static function create(WitResponse $response) { $data = $response->getDecodedBody(); $code = isset($data['code']) ? $data['code'] : null; if (isset($data['error'])) { $message = $data['error']; } else { if (isset($data['errors'])) { foreach ($data['errors'] as $error) { $message = $error; } } else { $message = 'Unknown error from Wit.'; } } switch ($code) { case 'no-auth': return new static($response, new WitAuthorizationException($message, 401)); } return new static($response, new WitOtherException($message, $code)); }
/** * Makes the request to Wit and returns the result. * * @param WitRequest $request * * @return WitResponse * * @throws WitSDKException */ public function sendRequest(WitRequest $request) { list($url, $method, $headers, $body) = $this->prepareRequestMessage($request); $timeOut = static::DEFAULT_REQUEST_TIMEOUT; // Should throw `WitSDKException` exception on HTTP client error. // Don't catch to allow it to bubble up. $rawResponse = $this->httpClientHandler->send($url, $method, $body, $headers, $timeOut); static::$requestCount++; $returnResponse = new WitResponse($request, $rawResponse->getBody(), $rawResponse->getHttpResponseCode(), $rawResponse->getHeaders()); if ($returnResponse->isError()) { throw $returnResponse->getThrownException(); } return $returnResponse; }