【问题标题】:Retrieve Error Message with array使用数组检索错误消息
【发布时间】:2013-03-22 01:11:48
【问题描述】:

我在 PHP 中使用 GoCardless's API 版本来处理我网站上的付款。但是,当他们的 API 返回错误时,我想向用户显示更有效的错误。

我已经完成了一半,但我想知道我是否可以做以下事情:

如果我有以下错误:

Array([error] => Array([0] =>资源已经确认))

有没有办法只用 PHP 提取 The resource has already been confirmed 部分?

我的代码:

    try{
        $confirmed_resource = GoCardless::confirm_resource($confirm_params);
    }catch(GoCardless_ApiException $e){
        $err = 1;
        print '<h2>Payment Error</h2>
        <p>Server Returned : <code>' . $e->getMessage() . '</code></p>';
    }

谢谢。

更新 1:

触发异常的代码:

$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_response_code < 200 || $http_response_code > 300) {

  // Create a string
  $message = print_r(json_decode($result, true), true);

  // Throw an exception with the error message
  throw new GoCardless_ApiException($message, $http_response_code);

}

更新 2 :-> print_r($e-&gt;getMessage()) 输出:

Array([error] => Array([0] =>资源已经确认))

【问题讨论】:

  • $errorArray['error'][0]

标签: php arrays exception gocardless


【解决方案1】:

$e-&gt;getMessage() 方法似乎返回一个索引为“错误”的数组,该数组又是一个包含消息文本的数组。如果你问我这是 糟糕的 API 设计

但是您可以像这样访问消息文本:

try{
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
    $err = 1;
    $message = $e->getMessage();
    $error = $message['error'];
    print '<h2>Payment Error</h2>
    <p>Server Returned : <code><' . $error[0] . "</code></p>";
}

【讨论】:

  • +1 为正确答案。但是对于 api 开发人员来说,返回一个数组而不是一个字符串是不好的。
  • 是的!我目前正在搜索文档。也许我会为此找到一个解释
  • 即使您找到了与 IMO 无关的解释......他们应该让 get message 返回一个包含所有错误的字符串,而不是一个数组,并且应该制定一种新方法来获取一组消息。 .. 或者类似的东西。
  • @prodigitalson 是的,我和你在一起。我浏览了源代码(简称),我发现GoCardless_ApiException-&gt;getMessage() 旨在返回一个字符串 - 但不要检查其构造函数中的参数类型。所以问题一定出在触发异常的代码中。如果没有更多信息,很难说出确切的位置。
  • 您查看我最近的更新了吗?它应该工作。 (如果我不是盲人;)
【解决方案2】:

如果您查看 GoCardless_ApiException 类代码,您会发现有一个 getResponse() 方法可用于访问响应数组的错误元素...

$try{
    $confirmed_resource = GoCardless::confirm_resource($confirm_params);
}catch(GoCardless_ApiException $e){
    $err = 1;
    $response = $e->getResponse();

    print '<h2>Payment Error</h2>
    <p>Server Returned : <code>' . $response['error'][0] . "</code></p>";
}

【讨论】:

    【解决方案3】:

    我发现了问题,$e-&gt;getMessage() 的输出是纯字符串,而不是数组。

    所以我将 Request.php 文件编辑为以下内容:

    $http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($http_response_code < 200 || $http_response_code > 300) {
    
      // Create a string <<-- THE PROBLEM -->>
      // $message = print_r(json_decode($result, true), true);
    
      $message_test = json_decode($result, true);
    
      // Throw an exception with the error message
      // OLD - throw new GoCardless_ApiException($message, $http_response_code);
      throw new GoCardless_ApiException($message_test[error][0], $http_response_code);
    
    }
    

    然后是我的 php 文件:

    try{
        $confirmed_resource = GoCardless::confirm_resource($confirm_params);
    }catch(GoCardless_ApiException $e){
        $err = 1;
        $message = $e->getMessage();
    
        print '<h2>Payment Error</h2>
        <p>Server Returned : <code>' . $message . "</code></p>";
    }
    

    页面输出:

    付款错误

    服务器返回:资源已经确认

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2021-08-16
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多