duanpie4763 2014-07-16 17:38
浏览 33
已采纳

对如何改善这个功能感到困惑..?

I've got the following function in my controller that handles preparing and loading my home page.

public function index()
    {
        // GetBalance
        $current_balance = $this->PayPal->getBalance();

        if(Session::has('errors'))
        {
            return Redirect::to('error');
        }

        // TransactionSearch
        $params = array(
            'number_of_days' => 1
        );
        $recent_history = $this->PayPal->transactionSearch($params);

        if(Session::has('errors'))
        {
            return Redirect::to('error');
        }

        // Make View
        $data = array('current_balance' => $current_balance, 'recent_history' => $recent_history);
        return View::make('index')->with('data', $data);
    }

As you can see, I'm making 2 different calls to the PayPal API through my model, and after each one I'm checking for an error. When errors do occur I flash the error messages and redirect to an error page accordingly.

I'd like to improve upon that so I don't have to keep using this same snippet of code over and over again when I'm making a bunch of calls prior to loading a view.

if(Session::has('errors'))
        {
            return Redirect::to('error');
        }

I tried moving this to its own function...

public function errorCheck()
    {
        if(Session::has('errors'))
        {
            return Redirect::to('error');
        }
    }

Then, I thought I could just do this within my index function...

// GetBalance
        $current_balance = $this->PayPal->getBalance();
        $this->errorCheck();

That doesn't work, though, I guess because errorCheck() is simply returning a value and not actually triggering the redirect, so I just end up at my home page with an error because none of the data it expects exists (since the API calls failed).

Any info on what I need to do here so that my errorCheck() function simply triggers the redirect when it should would be greatly appreciated.

Thanks!

  • 写回答

3条回答 默认 最新

  • doupu7651 2014-07-16 17:51
    关注

    The only way I can think of to avoid this is via the use of exceptions.

    With your errorCheck() method you could try this:

    // GetBalance
    $current_balance = $this->PayPal->getBalance();
    return $this->errorCheck();
    

    but that's not what you want... it's going to exit your method after the first call. What you really need is a way to catch an error wherever it occurs and handle it - and that's what exceptions do.

    I'm going to assume your PayPal class is a third-party package and you can't rewrite it to throw exceptions. Given that assumption, what you can do is this:

    Rewrite your errorCheck() method like so:

    public function errorCheck()
    {
        if(Session::has('errors'))
        {
            throw new Exception("Problem with Paypal!");
        }
    }
    

    Then wrap all your Paypal access code in a try/catch block:

    public function index()
    {
        try {
            // GetBalance
            $current_balance = $this->PayPal->getBalance();
    
            $this->errorCheck();
    
            // TransactionSearch
            $params = array(
                'number_of_days' => 1
            );
            $recent_history = $this->PayPal->transactionSearch($params);
    
            $this->errorCheck();
    
            // Make View
            $data = array('current_balance' => $current_balance, 'recent_history' => $recent_history);
            return View::make('index')->with('data', $data);
        } catch(Exception $e) {
            return Redirect::to('error');
        }
    }
    

    Each time you call errorCheck() it will check for an error and throw an exception. In that case, execution will immediately jump to the catch block and redirect to the error page.

    A better solution would be to throw the exceptions closer to the source of the error, ie. somewhere in the Paypal class when the error occurs. The idea here is that the exception includes a lot of useful information telling you what happened, like a stack trace. In the code I've given, the stack trace is going to show that the exception was thrown in the errorCheck() method which, while true, is not really helpful. If the exception could be thrown somewhere in the Paypal class, it would give you a better indication of what really went wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记