Welcome Guest, Not a member yet? Register   Sign In
curlrequest error handling
#1

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.
Reply
#2

Use a try/catch to catch the exception.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(This post was last modified: 06-19-2021, 12:54 AM by chakycool.)

Didn't work includebeer Sad
I Still get the same error on a 400 response.

CodeIgniter\HTTP\Exceptions\HTTPException
22 : The requested URL returned error: 400

SYSTEMPATH\HTTP\CURLRequest.php at line 796
Reply
#4

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
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

(This post was last modified: 06-19-2021, 11:15 PM by chakycool.)

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.
Reply
#6

Glad it's working now ! Namespaces can be tricky !  Cool
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB