/** * Parses a ``ClientException`` for any specific exceptions thrown by the * API in the response body. If the response body is not in JSON format, * an ``Exception`` is returned. * * Check a ``ClientException`` to see if it has an associated * ``ResponseInterface`` object. * * @param ClientException $exception * @return Exception */ protected function resolveExceptionClass(ClientException $exception) { $response = $exception->getResponse()->getBody(); $response = json_decode($response->getContents()); if ($response === null) { return new Exception($exception->getMessage()); } $meta = isset($response->meta) ? $response->meta : $response; $class = '\\Larabros\\Elogram\\Exceptions\\' . $meta->error_type; return new $class($meta->error_message); }
public function postCheck(Request $request) { $this->validate($request, ['number' => 'required']); $device = JWTAuth::parseToken()->toUser(); $countryCode = strtoupper($device->country_code); if ($device->credits <= 0) { throw new \Exception("Insufficient balance", 403); } $client = new Client(); $res = $client->get('https://lookups.twilio.com/v1/PhoneNumbers/' . $request->input('number'), ['auth' => [env('TWILIO_KEY'), env('TWILIO_SECRET')], 'query' => ['Type' => 'carrier', 'CountryCode' => $countryCode], 'exceptions' => false]); if ($res->getStatusCode() == 200) { $device->credits--; $device->save(); $response = json_decode($res->getBody(), true); if ($response['carrier']['name'] == null) { return ['error' => 404, 'credits' => $device->credits]; } return ['country_code' => $response['country_code'], 'phone_number' => $response['phone_number'], 'carrier_name' => $response['carrier']['name'], 'number_type' => $response['carrier']['type'], 'credits' => $device->credits]; } else { if ($res->getStatusCode() == 404) { return ['error' => 404, 'credits' => $device->credits]; } else { throw new \Exception(ClientException::getMessage(), 500); } } }
/** * @param ClientException $exception * @return \Exception */ protected function resolveExceptionClass(ClientException $exception) { $response = $exception->getResponse()->getBody(); $response = json_decode($response->getContents()); if ($response === null) { return new \Exception($exception->getMessage()); } if (!empty($response->message)) { $message = (string) $response->message; } elseif (!empty($response->errors)) { $message = $response->errors; if (is_array($response->errors)) { $message = []; foreach ($response->errors as $error) { $message[] = $error->message; } $message = implode(PHP_EOL, $message); } } else { $message = (string) $response; } return new \Exception($message); }
/** * Handle http client exceptions * * @param HttpClientException $e * * @return void * @throws Exception */ private function handleRequestException(HttpClientException $e) { if ($response = $e->getResponse()) { $exception = new Exception($response->getReasonPhrase(), $response->getStatusCode(), $e); $exception->setBody(json_decode($response->getBody())); throw $exception; } throw new Exception($e->getMessage(), 500, $e); }
/** * @param HttpClientException $exception * @throws TransportException */ protected function handleClientException(HttpClientException $exception) { switch ($exception->getResponse()->getStatusCode()) { case 401: throw new UnauthorizedException("Unauthorized API request."); default: throw new ClientException($exception->getMessage()); } }
/** * Method to handle the selection of the correct exception to throw * * @param \GuzzleHttp\Exception\ClientException $e - Guzzle Client Exception * @return mixed - Exception to throw */ protected function handleException(ClientException $e) { switch ($e->getCode()) { case 401: return new UnauthorizedRequestException($e->getMessage()); break; case 404: return new MissingResourceException($e->getMessage()); break; case 429: return new RateLimitExceededException($e->getMessage()); break; default: return new RuntimeException($e->getMessage()); } }
public static function create(\GuzzleHttp\Exception\ClientException $e) { $message = sprintf('Request could not be processed (%s %s): %s on %s', $e->getResponse()->getStatusCode(), $e->getResponse()->getReasonPhrase(), $e->getMessage(), $e->getRequest()->getUri()); return new self($message, $e->getResponse()->getStatusCode(), $e); }
/** * @param ClientException $e * * @return RequestException */ private function getCustomException(ClientException $e) { switch ($e->getCode()) { case 400: return new WadifyBadRequestException($e->getMessage(), $e->getRequest(), $e->getResponse()); case 401: return new WadifyAuthenticationException($e->getMessage(), $e->getRequest(), $e->getResponse()); case 403: return new WadifyAuthorizationException($e->getMessage(), $e->getRequest(), $e->getResponse()); default: return $e; } }
protected static function extractOriginalExceptionMessage(ClientException $exception) { self::$response = $exception->getResponse(); self::$stream = self::$response->getBody(); self::$content = self::$stream->getContents(); if (!self::$content) { throw new InstagramServerException($exception->getMessage(), $exception->getCode(), $exception); } return json_decode(self::$content); }
public function handleClientException(ClientException $e, $data = []) { $contents = json_decode($e->getResponse()->getBody()->getContents()); $exception = new RemoteException($e->getMessage()); $exception->data = $data; $exception->headers = $e->getRequest()->getHeaders(); $exception->setErrorCode(isset($contents->errors) ? $contents->errors[0]->code : null); $exception->setError(isset($contents->errors) ? $contents->errors[0]->description : ''); throw $exception; }