【发布时间】:2016-08-13 08:12:34
【问题描述】:
我正在尝试使用库 simple_salesforce 查询 Salesforce 中的对象。如果查询无效,我会收到 traceback 错误,我可以使用 try, except 语句将其隔离。在此示例中,Contactd 不是要查询的真实表。我真的很想隔离错误消息本身,但是 e 是一个类实例,所以我不确定如何隔离。
我的代码:
from simple_salesforce import Salesforce
sf = Salesforce(username='',
password='',
security_token='',
sandbox='')
try:
print sf.query_all("SELECT Id FROM Contactd")
except Exception as e:
print type(e)
print e
输出:
<class 'simple_salesforce.api.SalesforceMalformedRequest'>
Malformed request https://cs42.salesforce.com/services/data/v29.0/query/?q=SELECT+Id+FROM+Contactd. Response content: [{u'errorCode': u'INVALID_TYPE', u'message': u"\nSELECT Id FROM Contactd\n ^\nERROR at Row:1:Column:16\nsObject type 'Contactd' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names."}]
期望的输出:
\nSELECT Id FROM Contactd\n ^\nERROR at Row:1:Column:16\nsObject type 'Contactd' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Please reference your WSDL or the describe call for the appropriate names."
还包括simple_salesforce 错误处理代码:
def _exception_handler(result, name=""):
"""Exception router. Determines which error to raise for bad results"""
try:
response_content = result.json()
except Exception:
response_content = result.text
exc_map = {
300: SalesforceMoreThanOneRecord,
400: SalesforceMalformedRequest,
401: SalesforceExpiredSession,
403: SalesforceRefusedRequest,
404: SalesforceResourceNotFound,
}
exc_cls = exc_map.get(result.status_code, SalesforceGeneralError)
raise exc_cls(result.url, result.status_code, name, response_content)
【问题讨论】:
标签: python python-2.7 exception error-handling salesforce