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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.