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.
我正在开发一个API包装器类,这是我做的第一个。在大多数情况下,它并不太难。达到我需要处理API返回的错误可能性的程度,但是我应该如何处理它们。
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.
外部文件将调用API类,即findVenueByLocationID($ locationID);然后,此函数将构造API调用的URL和方法(POST,GET,DELETE等),并将其传递给名为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.
MakeCall构造完成的URL,将请求发送到服务并传回生成的XML。如果API返回错误,则它在返回的XML中。使用函数file_get_contents()调用URL。 API具有一定数量的错误代码,它将在XML中返回。
As I understand it, I should do the following during the function makeCall:
据我了解,我应该在函数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)
- 在返回XML之前,检查它是否包含错误代码,如果是,则将其传递给错误处理类以处理错误。 (记录并返回客户端版本错误消息)
- add a try catch around the file_get_contents() function to catch any connection errors, ie not being able to access the server?
- 在file_get_contents()函数周围添加一个try catch来捕获任何连接错误,即无法访问服务器?
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?
这被认为是最好的做事方式吗?我是否应该在调用makeCall时添加try catch而不是在file_get_contents内部添加?我应该为XML返回的每个错误抛出异常并使用错误类处理它们吗?
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.
我正在寻找的那种答案还应该包含一个资源的链接,该链接解释了一些围绕使用API包装器或类似事件进行错误处理的最佳实践。
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?
编辑:与我们的CTO谈话后,当前版本的PHP错误是异常,我应该抛出异常并将异常处理留给调用者。请记住,我正在为API实现一个包装类。思考?
1 个解决方案
#1
3
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.
因此,如果您从第三方获取XML,您可能希望它是有效的。但可能会有错误。如果API给你一个错误(找不到位置),那么你可能也会出错。只有在特殊情况下(你已经硬编码了一个你知道肯定会永远存在的位置),这可能是一个例外。
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.
最微不足道的例外是连接错误:那肯定是错误的。另一个简单的事情是您可能期望的API中的错误,例如“没有新信息”(仅作为示例):这是内部错误。在某些地方你必须画一条线,但在大多数情况下,有点清楚什么是例外,什么只是可能发生的错误。
#1
3
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.
因此,如果您从第三方获取XML,您可能希望它是有效的。但可能会有错误。如果API给你一个错误(找不到位置),那么你可能也会出错。只有在特殊情况下(你已经硬编码了一个你知道肯定会永远存在的位置),这可能是一个例外。
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.
最微不足道的例外是连接错误:那肯定是错误的。另一个简单的事情是您可能期望的API中的错误,例如“没有新信息”(仅作为示例):这是内部错误。在某些地方你必须画一条线,但在大多数情况下,有点清楚什么是例外,什么只是可能发生的错误。