Yii: Catching all exceptions for a specific controller

前端 未结 5 1787
日久生厌
日久生厌 2020-12-25 15:08

I am working on a project which includes a REST API component. I have a controller dedicated to handling all of the REST API calls.

Is there any way to catch all exc

5条回答
  •  臣服心动
    2020-12-25 15:26

    I'm using the following Base controller for an API, it's not stateless API, mind you, but it can serve just aswell.

    class BaseJSONController extends CController{

            public $data = array();
    
            public $layout;
    
            public function filters()
            {
                    return array('mainLoop');
            }
    
            /**
             * it all starts here
             * @param unknown_type $filterChain
             */
            public function filterMainLoop($filterChain){
                    $this->data['Success'] = true;
                    $this->data['ReturnMessage'] = "";
                    $this->data['ReturnCode'] = 0;
                    try{
                            $filterChain->run();
    
                    }catch (Exception $e){
                            $this->data['Success'] = false;
                            $this->data['ReturnMessage'] = $e->getMessage();
                            $this->data['ReturnCode'] = $e->getCode(); 
                    }
    
                    echo json_encode($this->data);
            }
    }
    

    You could also catch dbException and email those, as they're somewhat critical and can show underlying problem in the code/db design.

提交回复
热议问题