Esempio n. 1
0
 /**
  * Generate a Response for the current Exception
  *
  * @uses   Kohana_Exception::response()
  * @return Response
  */
 public function get_response()
 {
     $this->check();
     // Use Kohana_Exception to get a response with a response body
     $response = Kohana_Exception::response($this);
     // Copy headers from $this->_response
     $response->headers((array) $this->_response->headers());
     // Add CORS Headers
     $this->add_cors_headers($response);
     return $response;
 }
Esempio n. 2
0
 /**
  * Generate a Response for the current Exception
  *
  * @uses   Kohana_Exception::response()
  * @return Response
  */
 public function get_response()
 {
     if (!IN_PRODUCTION) {
         return Kohana_Exception::response($this);
     }
     $code = $this->getCode();
     try {
         $view = View::factory('error/error');
         $view->code = $code;
         $response = Response::factory()->status($code)->body($view->render());
     } catch (Exception $e) {
         $response = Kohana_Exception::response($this);
     }
     return $response;
 }
Esempio n. 3
0
 public static function handler($e)
 {
     try {
         Kohana_Exception::log($e);
         if (PHP_SAPI == 'cli') {
             $response = Kohana_Exception::text($e);
         } else {
             $response = Kohana_Exception::response($e);
         }
         echo $response;
         exit(1);
     } catch (Exception $e) {
         ob_get_level() and ob_clean();
         header('Content-Type: text/plain; charset=utf-8', TRUE, 500);
         echo Kohana_Exception::text($e);
         exit(1);
     }
 }
Esempio n. 4
0
 /**
  * Exception handler, logs the exception and generates a Response object
  * for display.
  *
  * @uses    Kohana_Exception::response
  *
  * @param   Exception $e
  *
  * @return  Response
  */
 public static function _handler(Exception $e)
 {
     try {
         // Log the exception
         Kohana_Exception::log($e);
         // Generate the response
         $response = Kohana_Exception::response($e);
         return $response;
     } catch (Exception $e) {
         /**
          * Things are going *really* badly for us, We now have no choice
          * but to bail. Hard.
          */
         // Clean the output buffer if one exists
         ob_get_level() and ob_clean();
         // Set the Status code to 500, and Content-Type to text/plain.
         header('Content-Type: text/plain; charset=' . Kohana::$charset, true, 500);
         echo Kohana_Exception::text($e);
         exit(1);
     }
 }
Esempio n. 5
0
 /**
  * Generate a Response for the current Exception
  * 
  * @uses   Kohana_Exception::response()
  * @return Response
  */
 public function get_response()
 {
     return Kohana_Exception::response($this);
 }
Esempio n. 6
0
 private function _response()
 {
     if (!isset(Response::$messages[$this->_response_httpCode])) {
         $this->_response_httpCode = 400;
     }
     if ($this->responseAsJSON) {
         if (!$this->isError()) {
             // SUCCESS
             $jsonData = $this->_response_data;
             if (NULL !== $this->responseLocation) {
                 //$this->response->headers('Location', $this->responseLocation);
             }
         } else {
             // ERROR
             if ($this->_response_data instanceof Exception) {
                 $exception = $this->_response_data;
                 $message = $exception->getMessage();
                 if (!$exception instanceof RestfulAPI_Exception) {
                     $this->_response_httpCode = $exception->getCode();
                 }
             } else {
                 $message = $this->_response_data;
             }
             if (!is_string($message)) {
                 $message = isset($exception) ? get_class($exception) : 'Undefined error';
             }
             $jsonData = $this->getErrorResponse($message, $this->_response_operationCode);
             if (isset($this->_response_errors)) {
                 $errors = [];
                 foreach ((array) $this->_response_errors as $field => $error) {
                     $errors[] = ['key' => $field, 'description' => $error];
                 }
                 $jsonData['parameters'] = $errors;
             }
         }
         $this->response->headers('cache-control', 'no-cache, no-store, max-age=0, must-revalidate');
         $this->response->headers('content-type', 'application/json; charset=utf-8');
         try {
             $this->response->status($this->_response_httpCode);
         } catch (Exception $e) {
             $this->response->status(500);
             $this->_response_operationCode = $this->_response_httpCode;
         }
         if (isset($exception)) {
             $_traceType = RestfulAPI::config('onerror.debug.exception', 'string');
             if ($_traceType) {
                 $this->setDebugData(['exception' => ['source' => self::exceptionString($exception), 'trace' => $_traceType !== 'array' ? $exception->getTraceAsString() : $exception->getTrace()]]);
             }
         }
         try {
             $this->setResponseBody($jsonData);
         } catch (Exception $e) {
             $exception = !isset($exception) ? $e : new Exception($e->getMessage(), $e->getCode(), $exception);
             $this->response->status(500);
             $this->setResponseBody(['message' => strtr('Error while formatting response:message', [':message' => !Helpers_Core::isProduction() ? ': ' . $e->getMessage() : '']), 'code' => 0]);
         }
         // LOGGING EXCEPTION
         if (isset($exception)) {
             $_logCodes = RestfulAPI::config('onerror.log.http_codes', []);
             if (TRUE === $_logCodes || Helpers_Arr::inArray($this->response->status(), $_logCodes)) {
                 Kohana::$log->add($this->response->status() == 500 ? Log::ERROR : Log::INFO, self::exceptionString($exception), NULL, ['exception' => $exception]);
             }
         }
         $this->response->send_headers(TRUE);
         exit($this->response);
     } else {
         if ($this->isError()) {
             if ($this->_response_data instanceof Exception) {
                 Kohana_Exception::response($this->_response_data);
             }
             throw HTTP_Exception::factory($this->_response_httpCode, '[ :errno ] :error', [':errno' => $this->_response_operationCode, ':error' => $this->_response_data]);
         } else {
             $this->response->body($this->_response_data);
         }
     }
 }