Andy-H Posted January 11, 2012 Share Posted January 11, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254793-handling-errors-in-an-client-api/ Share on other sites More sharing options...
trq Posted January 11, 2012 Share Posted January 11, 2012 Exceptions are probably the best option because you can easily send a descriptive message along with them. You just need to document the client well enough that users are aware it will throw exceptions on errors. Quote Link to comment https://forums.phpfreaks.com/topic/254793-handling-errors-in-an-client-api/#findComment-1306440 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.