Skip to main content

When should I throw an exception as opposed to return an error in PHP?


I'm working on an API wrapper class, which is the first I've made. For the most part it hasn't been too difficult. Getting to the point where I need to deal with the potential of errors being returned by the API, however confused as to how I should go about dealing with them.



An external file will make a call to the API class, ie, findVenueByLocationID($locationID); This function will then construct the URL and method of API call (POST, GET, DELETE, etc) and pass that to a function called makeCall.



MakeCall constructs the completed URL, sends the request to the service and passes back the resulting XML. If the API returns an error, it is within the XML it returns. The URL is called using the function file_get_contents(). The API has a set number of error codes it will return in the XML.



As I understand it, I should do the following during the function makeCall:



  • Before the XML is returned, check to see if it contains an error code, and if so, pass that to an error handling class to deal with the error. (Log and return client version error message)

  • add a try catch around the file_get_contents() function to catch any connection errors, ie not being able to access the server?



Is that considered the best way to do things? Should I be adding a try catch around the call to makeCall rather than inside it round file_get_contents? Should I be throwing an exception for each error returned by the XML and handling them with an error class?



The sort of answer I'm looking for should also contain a link to a resource explaining some best practices surrounding error handling with API wrappers or such things.



Thanks in advance for your time and responses.





EDIT: After talking with our CTO, errors in PHP at current version ARE exceptions, and I should throw exceptions and leave the dealing of the exceptions to the caller. Remember, I'm implementing a wrapper class for an API. Thoughts?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You should first know the difference between exceptions and errors: errors happen, exceptions are exceptional.

    For instance, a user who types a wrong password (cannot log in) gets an error. When the database is not available while checking the password, you would get an exception (and hopefully gracefull handling of that exception).

    So if you get XML from a third party you would probably expect it to be valid. But there could be errors. If the API gives you an error (location not found) it will probably be an error on your side too. Only in special cases (you've hardcoded a locatoin you know for sure will always be there) that might be an exception.

    The most trivial exceptions are the connection errors: there is most defeniately something wrong then. The other easy thing are errors in the API that you could expect, like "no new information" (Just as an example): This is an internal error. And somewhere you must draw a line, but in most cases it is kinda clear what is exceptional and what is just an error that can occur.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex