Jump to content

Handling errors in an client API


Andy-H

Recommended Posts

I am writing an API for clients to use to integrate with an server API I have written and it will be used in a variety of different environments and used by people with varying coding styles (hopefully), I was just wondering how I should handle errors? I was going to throw an exception, but what if people don't use a try / catch ? Should I return false to allow them to handle an error?


   private function _request($uri)
   {
      if ( ini_get('allow_url_fopen') )
      {
         $this->_response = file_get_contents($uri);
         return false;
      }
      if ( function_exists('curl_init') )
      {
         $ch = curl_init($uri);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         $this->_response = curl_exec($ch);
         curl_close($ch);
         return false;
      }
      throw new Exception('Allow URL fopen disabled and cURL disabled');
   }

 

 

Also, any scenarios where the above would fail to return a result (apart from the obvious lol)

 

 

Thanks, Andy

Link to comment
https://forums.phpfreaks.com/topic/254793-handling-errors-in-an-client-api/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.