在CodeIgniter中处理纯PHP错误
我有一个使用CodeIgniter开发的API的移动应用程序,这个API按条带执行支付 这些付款是递归的,所以我每月向用户收费一次。但有些用户不能收费(例如,如果用户的卡上没有足够的资金),在这种情况下,Stripe会抛出一个异常 我希望能够在我的控制器中执行try/catch。目前,我有以下代码:在CodeIgniter中处理纯PHP错误,php,codeigniter,Php,Codeigniter,我有一个使用CodeIgniter开发的API的移动应用程序,这个API按条带执行支付 这些付款是递归的,所以我每月向用户收费一次。但有些用户不能收费(例如,如果用户的卡上没有足够的资金),在这种情况下,Stripe会抛出一个异常 我希望能够在我的控制器中执行try/catch。目前,我有以下代码: try { $charge = \Stripe\Charge::create(array( "amount" => xxx, "currency" =>
try {
$charge = \Stripe\Charge::create(array(
"amount" => xxx,
"currency" => "eur",
"customer" => xxx)
);
} catch (Exception $e) {
// If user has insufficient funds, perfom this code
}
我最近看到catch中的代码从未执行过,我看到CI有自己的错误处理系统,我可以在日志中看到cli运行的控制器的错误显示在这个视图上:application/views/errors/cli/error\u php
。那么,如何简单地检测条带代码是否返回异常呢
提前感谢您的回答:)尝试对您的异常使用全局命名空间,如下所示: catch(\Exception$e)//注意反斜杠
尝试对异常使用全局命名空间,如下所示: catch(\Exception$e)//注意反斜杠
请仔细参阅Stripe的API文档 他们以非常详细的方式描述了错误处理。您可以在此处阅读:
请仔细参阅Stripe的API文档 他们以非常详细的方式描述了错误处理。您可以在此处阅读:
所以你只想回显一个错误,它发生了吗<代码>打印“错误!:”$e->getMessage()。“
”;模具()代码>所以你只想回显一个错误,它是否发生了<代码>打印“错误!:”$e->getMessage()。“
”;模具()代码>我尝试了\,但并没有改变什么。即使我只是回显一个未声明的var,catch中的代码也不会执行回显一个未声明的var将产生一个错误-它不会引发异常。PHP就是这样设计的。要测试异常,请将“amount”值更改为类似“xxx”的荒谬值,并尝试进行测试条带事务。非常感谢您提供的信息!事实上,如果我改变条带数量,我最终会在cacth中输入:)我尝试了\,但它没有改变任何东西。即使我只是回显一个未声明的var,catch中的代码也不会执行回显一个未声明的var将产生一个错误-它不会引发异常。PHP就是这样设计的。要测试异常,请将“amount”值更改为类似“xxx”的荒谬值,并尝试进行测试条带事务。非常感谢您提供的信息!事实上,如果我更改条带数量,我最终会在cacth中输入:)
try {
// Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() . "\n");
print('Type is:' . $err['type'] . "\n");
print('Code is:' . $err['code'] . "\n");
// param is '' in this case
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
}