【发布时间】:2017-12-10 07:28:25
【问题描述】:
我正在处理来自 Stripe API 的错误 - 使用 Stripe 文档中提供的标准 try/catch 块一切正常:
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
【问题讨论】:
标签: php error-handling try-catch stripe-payments abstraction