我从条纹API处理错误 - 一切使用的条纹文档提供的标准try/catch块正常工作:抽象的try/catch PHP - 条纹错误处理
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
感谢您的回答 - 这是非常有用的。我的主要问题是传递方法,因为try块会有很大的差异。它可能会收取费用,检索客户,创建订阅,更改订阅计划,添加优惠券 - 所有这些都有不同的参数和元数据。 – Ryan
一个匿名函数应该是最灵活的。 – deceze
谢谢 - 我会研究一下 – Ryan