php - 抽象 try/catch PHP - strip 错误处理

标签 php error-handling try-catch stripe-payments abstraction

我正在处理来自 Stripe API 的错误 - 使用 Stripe 文档中提供的标准 try/catch block 一切正常:

try {

  // Use Stripe's library to make requests...

} catch(\Stripe\Error\Card $e) {

  //card errors

  $body = $e->getJsonBody();
  $err  = $body['error'];

  print('Status is:' . $e->getHttpStatus() . "\n");
  print('Type is:' . $err['type'] . "\n");
  print('Code is:' . $err['code'] . "\n");
  print('Param is:' . $err['param'] . "\n");
  print('Message is:' . $err['message'] . "\n");

} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

但是,这是很多代码,我发现自己在重复它。我对如何整理它有点困惑。像这样的东西是理想的:

try {

  // Use Stripe's library to make requests...

} // catch all errors in one line

最佳答案

有一个函数可以为您处理它:

function makeStripeApiCall($method, $args) {
    try {
        // call method
    } catch (...) {
        // handle error type 1
    } catch (...) {
        // handle error type 2
    } ...
}

现在:

  1. 如何传递$method?有几种方法可以做到这一点;例如:

    $method = 'charge';
    $this->stripe->{$method}($args);
    
    $method = [$stripe, 'charge'];
    call_user_func_array($method, $args);
    

    $method = function () use ($stripe, $args) { return $stripe->charge($args); };
    $method();
    

    选择最适合您情况的选项。

  2. 如何准确处理错误?

    您应该捕获特定的 Stripe 异常,并根据需要将它们转换为您自己的内部异常类型。您需要以不同的方式处理几种广泛类型的问题:

    1. 错误的请求,例如卡被拒绝:您希望直接捕获调用业务逻辑代码中的这些错误,并根据具体问题执行某些操作

    2. 服务中断,例如Stripe\Error\ApiConnection 或速率限制:除了稍后重试之外,您不能对这些做太多事情,您需要在更高的位置捕获这些错误并向用户显示“抱歉,请尝试”稍后再来”消息

    3. 配置错误,例如Stripe\Error\Authentication:没有什么可以自动完成的,您可以向用户显示 500 HTTP 服务器错误,敲响警钟并让开发人员修复身份验证 key

    这些基本上是您想要在内部定义然后根据需要捕获它们的异常类型。例如:

    ...
    catch (\Stripe\Error\ApiConnection $e) {
        trigger_error($e->getMessage(), E_USER_WARNING);
        throw new TransientError($e);
    }
    ...
    

完成这一切之后,您将减少 API 调用,如下所示:

try {
    return makeStripeApiCall('charge', $args);
} catch (BadRequestError $e) {
    echo 'Card number invalid: ', $e->getMessage();
}
// don't catch other kinds of exception here,
// let a higher up caller worry about graver issues

关于php - 抽象 try/catch PHP - strip 错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946372/

相关文章:

javascript - jQuery Datatables 根据条件更改列的值

error-handling - 如何正确使用 Option::ok_or() 方法?

javascript - Try-Catch 在 TestComplete 中不起作用

php - 对对象数组使用 $casts

javascript - 通过ajax对Mysql表进行排序

PHP fatal error : Call to a member function diff() on a non-object in. .. 比较数据库中的日期时间

ios - 在对象初始化时为错误的参数值Objective-C引发错误

asp.net-mvc - database.SaveChanges()抛出EntityValidationException

c++ - __try/__except block 或 try/catch block 哪个更好用?

exception - 有什么办法可以在 Swift 中捕获断言吗?