我從條紋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