dsa5211314 2016-11-02 09:05
浏览 43

在生产环境中使用异常:尝试捕获阻止“返回”布尔值?

The app is built on the MVC pattern, so my controllers are separate from my models.

I am using two payment gateways, Stripe and PayPal and have moved the API code into a model called Payment_Model.php.

Both functions have huge try/catch blocks that throw all manner of errors when a payment fails which is a good thing for me, not so for a customer...

Here is a try catch block example

     try {

        Stripe::setApiKey($this->config->item('stripe_secret_key'));

        $customer = Customer::create([
            'email'  => 'customer@example.com',
            'source' => $this->input->post('stripe_token'),
        ]);

        $charge = Charge::create([
            'customer'    => $customer->id,
            'amount'      => $option->price,
            'currency'    => 'eur',
            "description" => "Demo Transaction", // @TODO
        ]);

    } catch (Exception $e) {

    } catch (Stripe_CardError $e) {
        throw new Exception($e);
    } catch (Stripe_InvalidRequestError $e) {
        throw new Exception($e);
    } catch (Stripe_AuthenticationError $e) {
        throw new Exception($e);
    } catch (Stripe_ApiConnectionError $e) {
        throw new Exception($e);
    } catch (Stripe_Error $e) {
        throw new Exception($e);
    } catch (Exception $e) {
        throw new Exception($e);
    }

I don't want to display these errors or exceptions in my production environment... instead I would like to replace throw new Exception($e) with false so that I can call the model function in my controller and if something goes wrong I can redirect the user to a decent error page...

So my question is this:

Can I return a boolean IF something bad is caught so that I can either redirect to a success page or an error page in my controller? Or am I missing the point of using exceptions?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗