-
yuma2020
Juan Manuel Pedro Villalba
-
Posts: 39
Threads: 19
Joined: Mar 2019
Reputation:
0
02-25-2021, 07:03 PM
(This post was last modified: 02-25-2021, 07:21 PM by yuma2020.)
how to implement custom exception management in CodeIgniter 4?
Function in the 200 is working all ok,
PHP Code: public function hx2getData($data) { if ($data->link_id) { $id = (string) $data->link_id; $url = 'https://www.hx2.xyz/api/v1/links/'.((string)$id); $client = \Config\Services::curlrequest();
$response = $client->request('GET', $url, [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps' ], 'timeout' => 5, ]); // $code = $response->getStatusCode(); // Code result : 200, 403 $reason = $response->getReason(); // if OK or Error try { if (strpos($response->getHeader('content-type'), 'application/json') !== false) { $result = json_decode($response->getBody()); } } catch (\Exception $e) { die($e->getMessage()); } return (object) $result ? (object) $result : 0; }
}
Calling:
Code: $this->getapi->hx2getData((object) $data);
but in HTTP status 404 gives me the following error is result:
PHP Code: { "title": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "type": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "code": 500, "message": "22 : The requested URL returned error: 404 ", "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\Exceptions\\HTTPException.php", "line": 100, "trace": [ { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 823, "function": "forCurlError", "class": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "type": "::", "args": [ "22", "The requested URL returned error: 404 " ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 449, "function": "sendRequest", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ { "10002": "https://www.hx2.xyz/api/v1/links/110", "19913": true, "42": true, "74": true, "-1": true, "64": true, "155": 5000, "156": 150000, "45": true, "10036": "GET", "10023": [ "Accept: application/json", "Authorization: Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps" ] } ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 158, "function": "send", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ "GET", "https://www.hx2.xyz/api/v1/links/110" ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\app\\Libraries\\GetLocateLibrary.php", "line": 40, "function": "request", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ "GET", "https://www.hx2.xyz/api/v1/links/110", { "headers": { "Accept": "application/json", "Authorization": "Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps" }, "timeout": 5 } ] }
(02-25-2021, 07:03 PM)yuma2020 Wrote: how to implement custom exception management in CodeIgniter 4?
Function in the 200 is working all ok,
PHP Code: public function hx2getData($data) { if ($data->link_id) { $id = (string) $data->link_id; $url = 'https://www.hx2.xyz/api/v1/links/'.((string)$id); $client = \Config\Services::curlrequest();
$response = $client->request('GET', $url, [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps' ], 'timeout' => 5, ]); // $code = $response->getStatusCode(); // Code result : 200, 403 $reason = $response->getReason(); // if OK or Error try { if (strpos($response->getHeader('content-type'), 'application/json') !== false) { $result = json_decode($response->getBody()); } } catch (\Exception $e) { die($e->getMessage()); } return (object) $result ? (object) $result : 0; }
}
Calling:
Code: $this->getapi->hx2getData((object) $data);
but in HTTP status 404 gives me the following error is result:
PHP Code: { "title": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "type": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "code": 500, "message": "22 : The requested URL returned error: 404 ", "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\Exceptions\\HTTPException.php", "line": 100, "trace": [ { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 823, "function": "forCurlError", "class": "CodeIgniter\\HTTP\\Exceptions\\HTTPException", "type": "::", "args": [ "22", "The requested URL returned error: 404 " ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 449, "function": "sendRequest", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ { "10002": "https://www.hx2.xyz/api/v1/links/110", "19913": true, "42": true, "74": true, "-1": true, "64": true, "155": 5000, "156": 150000, "45": true, "10036": "GET", "10023": [ "Accept: application/json", "Authorization: Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps" ] } ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\system\\HTTP\\CURLRequest.php", "line": 158, "function": "send", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ "GET", "https://www.hx2.xyz/api/v1/links/110" ] }, { "file": "D:\\laragon\\www\\marketineros\\V3\\app\\Libraries\\GetLocateLibrary.php", "line": 40, "function": "request", "class": "CodeIgniter\\HTTP\\CURLRequest", "type": "->", "args": [ "GET", "https://www.hx2.xyz/api/v1/links/110", { "headers": { "Accept": "application/json", "Authorization": "Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps" }, "timeout": 5 } ] }
Sorry stupid error is solved with move of line try
PHP Code: public function hx2getData($data) { if ($data->link_id) { try { $id = (string) $data->link_id; $url = 'https://www.hx2.xyz/api/v1/links/'.((string)$id); $client = \Config\Services::curlrequest();
$response = $client->request('GET', $url, [ 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ZDEcp8bptOz3Hi6RFXGry0JnUmbqvNC316IRbUOrwXH7Z85PVXEhnGTutcps' ], 'timeout' => 5, ]); // $code = $response->getStatusCode(); // Code result : 200, 403 $reason = $response->getReason(); // if OK or Error
if (strpos($response->getHeader('content-type'), 'application/json') !== false) { $result = json_decode($response->getBody()); } } catch (\Exception $e) { die($e->getMessage()); } return (object) $result ? (object) $result : 0; }
}
-
InsiteFX
Super Moderator
-
Posts: 6,574
Threads: 331
Joined: Oct 2014
Reputation:
240
-
yuma2020
Juan Manuel Pedro Villalba
-
Posts: 39
Threads: 19
Joined: Mar 2019
Reputation:
0
|