【问题标题】:Recurly PHP client. How to customize error messages?递归 PHP 客户端。如何自定义错误消息?
【发布时间】:2016-07-04 14:51:32
【问题描述】:

我正在尝试自定义错误消息。 为了处理错误,我根据this Recurly 文档使用了“try/catch”块,例如:

try {
  $account = Recurly_Account::get('my_account_id');
  $subscription = new Recurly_Subscription();
  $subscription->account = $account;
  $subscription->plan_code = 'my_plan_code';
  $subscription->coupon_code = 'my_coupon_code';
  /* .. etc .. */
  $subscription->create();
}
catch (Exception $e) {
  $errorMsg = $e->getMessage();
  print $errorMsg;
}

我想在 catch 块中使用这样的代码:

catch (Exception $e) {
  $errorCode = $e->getCode();
  print $myErrorMsg[$errorCode]; // array of my custom messages.
}

但是对于所有可能的错误,getCode() 方法总是返回零。

我对 Recurly 团队(或该主题中的谁)的问题: 我如何获得错误的错误代码?或者请解释我如何解决这个话题。谢谢!

【问题讨论】:

    标签: php recurly


    【解决方案1】:

    如果您查看 Github 上的 PHP 客户端并搜索“throw new”,这是抛出异常时所做的事情,您会看到他们没有将异常错误代码设置为异常的第二个参数构造方法。

    Github 上的递归 PHP 客户端: https://github.com/recurly/recurly-client-php/search?utf8=%E2%9C%93&q=throw+new

    PHP 异常文档: http://php.net/manual/en/language.exceptions.extending.php

    因此,您要么需要根据名称捕获更多异常 即

    catch (Recurly_NotFoundError $e) {
      print 'Record could not be found';
    }
    

    查看异常信息并进行比较

    catch (Exception $e) {
      $errorMessage = $e->getMessage();
      if($errorMessage=='Coupon is not redeemable.')
      {
        $myerrorCode=1;
      }
      //Add more else if, or case switch statement to handle the various errors you want to handle
      print $myErrorMsg[$myerrorCode]; // array of my custom messages.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多