【问题标题】:Isolating the error message in error handling from Salesforce API从 Salesforce API 中隔离错误处理中的错误消息
【发布时间】: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


    【解决方案1】:

    要“隔离错误消息本身”并获得所描述的所需输出,我们可以导入SalesforceMalformedRequest,然后像这样使用e.content(在Python 3.5 中)...

    from simple_salesforce import Salesforce
    from simple_salesforce.exceptions import SalesforceMalformedRequest
    
    sf = Salesforce(...)
    
    try:
        print(sf.query_all("SELECT Id FROM Contactd"))
    except SalesforceMalformedRequest as e:
        print(type(e.content))
        print(e.content)
    

    ...从中我们看到类型是'list',并且列表包含dict;因此,要获得所需的字符串,我们可以使用:

    print(e.content[0]['message'])
    

    附言在解决这个问题时,我也在 github 上遇到了这个问题:exception_handler provides inconsistent API #215

    【讨论】:

      【解决方案2】:

      API 调用返回错误数据,您的客户端应用程序可以使用这些数据来识别和解决运行时错误。如果在调用大多数 API 调用过程中发生错误,则 API 提供以下类型的错误处理:

      对于格式错误的消息、身份验证失败或类似问题导致的错误,API 会返回带有关联 ExceptionCode 的 SOAP 错误消息。 对于大多数调用,如果由于特定于查询的问题而发生错误,API 将返回错误。例如,如果一个 create() 请求包含超过 200 个对象。

      当您通过 login() 调用登录时,会开始一个新的客户端会话并生成相应的唯一会话 ID。会话在预定的不活动时间后自动过期,可以通过单击安全控制在 Salesforce 从设置中进行配置。默认值为 120 分钟(两小时)。如果您进行 API 调用,则不活动计时器将重置为零。

      当您的会话到期时,将返回异常代码 INVALID_SESSION_ID。如果发生这种情况,您必须再次调用 login()。

      【讨论】:

      • 感谢您的解释。我想我已经达到了我已经识别出第二段中提到的错误的地步,但只需要隔离其中的一部分。
      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 1970-01-01
      • 2017-06-10
      • 2017-07-28
      • 2017-06-03
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      相关资源
      最近更新 更多