Welcome Guest, Not a member yet? Register   Sign In
how to implement custom exception management in CodeIgniter 4?
#1

(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
                
                
            }

        
    } 
Reply
#2

You can read about Custom Exceptions here.

CodeIgniter 4 User Guide - Custom Exceptions
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(02-25-2021, 09:47 PM)InsiteFX Wrote: You can read about Custom Exceptions here.

CodeIgniter 4 User Guide - Custom Exceptions
Thank you very much for the info. Greetings to the community
Reply
#4

I don't see in what way these are "custom". They appear to be already defined in CI core. I'd like to define my own exceptions somewhere.
Reply
#5

While that functionality is something I'd like to see expanded on in the framework's future, there is no global custom exception handling. You might be able to attach your own handler during system boot by registering it with set_error_handler but that might cause conflicts with the one that we already register. I'm not 100% sure.

I see that you fixed your immediate error, though. And using try/catch blocks and not relying on a global handler is what I was going to recommend anyway.
Reply
#6

(11-12-2021, 11:16 AM)kilishan Wrote: While that functionality is something I'd like to see expanded on in the framework's future, there is no global custom exception handling. You might be able to attach your own handler during system boot by registering it with set_error_handler but that might cause conflicts with the one that we already register. I'm not 100% sure.

I see that you fixed your immediate error, though. And using try/catch blocks and not relying on a global handler is what I was going to recommend anyway.

I am interested in your opinion about this PR https://github.com/codeigniter4/CodeIgniter4/pull/5355
Reply
#7

Perfect timing - I just left a response Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB