How to catch errors in assistant api through curl method?

I am using curl with a PHP application. I tried to catch error while creating an assistant. It was stuck and no response from curl. Below is sample code.


function curl_request($url, $data = '')
{
    $apiKey = "sk-....";
  
    $ch = curl_init($url);
    
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        if ($data) {
            $data = json_encode($data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $apiKey,
        'OpenAI-Beta: assistants=v1'
    ]);
    
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); 

    $result = curl_exec($ch);
     curl_close($ch);

    if (curl_errno($ch)) {
      //print_r(curl_error($ch), true) ;
         return curl_error($ch);
    }

     $result = json_decode($result, true);
    if (isset($result['error']) && !empty($result['error'])) {
       print_r($result['error'], true);
      return $result['error'];
    }
    
    return $result;
}

Not sure what else I missing.