curlrequest error handling |
Hi,
I got the "curlrequest" service working just fine but if there is an error such as a invalid login which ends in a 400 Bad request I get a codeignitor error "CodeIgniter\HTTP\Exceptions\HTTPException". How can I get this error to the response other than getting a cogeigniter error.
What do you mean by “didn’t work”?
Show your code and the error message if any… Here’s an example how to use try/catch: http://codeigniter.com/user_guide/genera...ight=catch
try {
$response = $this->getToken($keys); print_r($response); } catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } private function getToken($data){ $body = '{"username":"[email protected]","password" : "'.$data->pw.'"}'; $client = \Config\Services::curlrequest(); $result = $client->setBody($body)->request('post', $data->url.'/v1/login/', [ 'headers' => [ 'Content-Type' => 'application/json', 'x-api-key' => $data->key ] ]); return json_decode($result->getBody()); } Sorry I didn't share the code before. Now on the code above I'm passing a invalid email and I end up with an error (CodeIgniter\HTTP\Exceptions\HTTPException). If all is good no errors. I found the problem Add the "\" to "Exception" making it " \Exception" Thank you so much incluebeer.
|
Welcome Guest, Not a member yet? Register Sign In |