예제 #1
0
파일: base.php 프로젝트: uzura8/flockbird
 protected function controller_common_api(callable $func)
 {
     try {
         $this->check_response_format($this->api_accept_formats);
         if (Input::method() != 'GET' && !$this->api_not_check_csrf) {
             Util_security::check_csrf();
         }
         $this->response_body = $func() ?: $this->response_body;
         // execute main.
         if (Site_Model::check_is_orm_obj($this->response_body)) {
             throw new \FuelException('Response body not allowed Orm obj.');
         }
         $status_code = 200;
     } catch (\HttpNotFoundException $e) {
         $status_code = 404;
     } catch (\ApiNotAuthorizedException $e) {
         $status_code = 401;
     } catch (\HttpForbiddenException $e) {
         $status_code = 403;
     } catch (\HttpMethodNotAllowed $e) {
         $status_code = 405;
     } catch (\HttpBadRequestException $e) {
         $status_code = 400;
     } catch (\HttpInvalidInputException $e) {
         $status_code = 400;
     } catch (\ValidationFailedException $e) {
         $this->response_body['errors']['message'] = Site_Controller::get_error_message($e);
         $status_code = 400;
     } catch (\DisableToUpdateException $e) {
         $this->response_body['errors']['message'] = $e->getMessage() ?: term('form.update') . 'が禁止されています。';
         $status_code = 400;
     } catch (\Database_Exception $e) {
         $this->response_body['errors']['message'] = Site_Controller::get_error_message($e, true);
         $status_code = 500;
     } catch (\FuelException $e) {
         $status_code = 500;
     } catch (\Exception $e) {
         $status_code = 500;
     }
     if ($status_code == 500) {
         if (!empty($e)) {
             Util_Toolkit::log_error($e->getMessage());
         }
         if (\DB::in_transaction()) {
             \DB::rollback_transaction();
         }
     }
     $response_body = Site_Controller::supply_response_body($this->response_body, $status_code, $this->format);
     return self::response($response_body, $status_code);
 }