Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:48:04+00:00 2026-05-22T11:48:04+00:00

I’m working on an API wrapper class, which is the first I’ve made. For

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:48:04+00:00Added an answer on May 22, 2026 at 11:48 am

    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 graceful 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 location you know for sure will always be there) that might be an exception.

    The most trivial exceptions are the connection errors: there is most definitely 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.