How do I handle coding errors in my PHP which has no GUI, only an API? I am concerned that I might never know that there are any. Note that only I will develop the client side. This is not a public API.

  • Do I claim that I tested so thoroughly that there are none?
  • Do I not handle them and wait for users to complain that the app isn't working?
  • Do I send an HTPP response 501, perhaps with some indication of what went wrong? If so, do I have a pop-up for the user, possibly asking them to contact me?
  • Or use a proprietary response code, since I am coding both sides of the AI and no one else ever will?
  • Or should I have my PHP code send me an email, with error details?
  • Any other approaches?

1 Answer 1


Error handling of an API is exactly the same as error handling of any web application. Exceptions should be logged to a central syslog server, and the central log should be reviewed on regular basis. If you use a framework, the framework should handle this for you.

Sending e-mails through PHP every time an exception occurs is a risky path. What if exceptions occur within the sender itself? What if SMTP server is down and you receive no e-mails for weeks? What if a hacker is intentionally generating thousands of exceptions per second?

The response code in a case of an exception should be HTTP 500 Internal Server Error. You are not expected to invent your own response codes, since the point of the API is to use a standard interface any developer can understand. If you use a framework, the framework should handle this for you.

Never claim that you tested your app so well that there are no bugs. Bugs will be found, and your company's reputation will suffer.

How client app should react to HTTP 500 depends on the application. It may inform the user that something went wrong. Or may retry. Or dispatch an AJAX request containing the error and the stack trace. Avoid popups, since they are annoying and will be dismissed most of the time. Don't ask the user to contact you: he is already annoyed by the fact that your application is not working, so you are not in a position of asking the user anything. On the other hand, apologizing and providing a contact address for support is nice.

  • Well, ok, I won't invent non-standard response codes, although that does seem a valid option, since only I am developing both client & Server sides. Maybe it would be cleaner to send a proprietary X- field in the 500 response which could provide additional information to my client? Feb 18, 2015 at 10:59
  • 1
    @Mawg: you don't need an X-field. Here's some details about how errors should be sent to the client. Feb 18, 2015 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.