HttpException can be differentiated via its [[statusCode]] property value which keeps a standard HTTP status code (e.g. 404, 500). Error handlers may use this status code to decide how to format the error page. Throwing an HttpException like in the following example will result in the 404 page to be displayed. php if ($item === null) { // item does not exist throw new \yii\web\HttpException(404, 'The requested Item could not be found.'); }
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends yii\base\UserException
コード例 #1
39
ファイル: SiteController.php プロジェクト: yujin1st/core
 /**
  * Redefine error message for own styling
  *
  * @return string
  */
 public function actionError()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = Yii::t('yii', 'An internal server error occurred');
     }
     if (Yii::$app->getRequest()->getIsAjax()) {
         return "{$name}: {$message}";
     } else {
         $this->layout = 'main';
         return $this->render('error', ['code' => $code, 'name' => $name, 'message' => $message, 'exception' => $exception]);
     }
 }
コード例 #2
0
 public function run()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
     }
     if (Yii::$app->getRequest()->getIsAjax()) {
         return "{$name}: {$message}";
     } else {
         if ($code === 403) {
             return Yii::$app->response->redirect('admin/login');
         }
         return $this->controller->render($this->view ?: $this->id, ['name' => $name, 'message' => $message, 'exception' => $exception]);
     }
 }
コード例 #3
0
 public function actionError()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
     }
     if (Yii::$app->getRequest()->getIsAjax()) {
         return "{$name}: {$message}";
     } else {
         return $this->render('error', ['name' => $name, 'message' => $message, 'exception' => $exception]);
     }
 }
コード例 #4
0
 /**
  * Runs the action
  *
  * @return string result content
  */
 public function run()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
     }
     $originalStatus = $exception->statusCode;
     $exception->statusCode = 200;
     $result = ['success' => false, 'originalStatus' => $originalStatus, 'name' => $name, 'message' => $message, 'exception' => $exception];
     //do not reverse
     if (Yii::$app->getRequest()->getIsAjax() || Yii::$app->getRequest()->getMethod() !== 'GET') {
         return Json::encode($result);
     } else {
         return $this->controller->render($this->view ?: $this->id, ['error' => $result]);
     }
 }
コード例 #5
0
 public function actionError()
 {
     \Yii::$app->response->format = Response::FORMAT_JSON;
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
         Yii::$app->response->statusCode = $code;
     } else {
         $code = $exception->getCode();
         Yii::$app->response->statusCode = 500;
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = Yii::t('yii', 'An internal server error occurred.');
     }
     return ['error' => $name, 'message' => $message];
 }
コード例 #6
0
 /**
  * Constructor.
  * @param array | json string $message error message
  */
 public function __construct($message = [])
 {
     if (is_array($message)) {
         $message = Json::encode($message);
     }
     parent::__construct(440, $message, null);
 }
コード例 #7
0
ファイル: MustHandleError.php プロジェクト: weiyiyi/base
 public function __construct($status, $message = null, $code = 0, \Exception $previous = null)
 {
     if ($message === null) {
         $message = isset(self::$messages[$status]) ? self::$messages[$status] : 'Error';
     }
     parent::__construct($status, $message, $code, $previous);
 }
 public function __construct($status = null, $message = null, $code = 0, \Exception $previous = null)
 {
     if (empty($status)) {
         $status = 403;
     }
     if (empty($message)) {
         $message = 'Berechtigungsfehler';
     }
     $this->statusCode = $status;
     parent::__construct($status, $message, $code, $previous);
 }
コード例 #9
0
 /**
  * Constructor.
  * @param Response $response
  * @param \Exception $previous The previous exception used for the exception chaining.
  */
 public function __construct(Response $response, \Exception $previous = null)
 {
     if ((int) $response->getStatusCode() == 401) {
         $errorCode = $response->getParameter('error', 'required_token');
         $message = $response->getParameter('error_description', 'An Access Token is required.');
     } else {
         $errorCode = $response->getParameter('error', 'unknown');
         $message = $response->getParameter('error_description', $response->getStatusText());
     }
     $this->errorCode = $errorCode;
     $this->errorUri = $response->getParameter('error_uri');
     return parent::__construct($response->getStatusCode(), $message, 0, $previous);
 }
コード例 #10
0
ファイル: ErrorAction.php プロジェクト: ChristopheBrun/hLib
 /**
  * Runs the action
  *
  * @return string result content
  */
 public function run()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = $this->defaultMessage ?: HLib::t('messages', 'An error occurred during the process');
     }
     if (Yii::$app->getRequest()->getIsAjax()) {
         // Requête AJAX : on renvoie juste de quoi identifier l'erreur
         return "{$name}: {$message}";
     }
     // Le modèle de page utilisé dépend du code de l'erreur. Par défaut, c'est 'error.php'
     switch ($code) {
         case '404':
             $this->view = 'error404';
             break;
         default:
             break;
     }
     return $this->controller->render($this->view ?: $this->id, compact('name', 'message', 'exception'));
 }
コード例 #11
0
 public static function convertExceptionToArray($exception)
 {
     $rpc = null;
     if (isset($exception->method) && $exception->method !== null) {
         $rpc = ['class' => $exception->method->class, 'method' => $exception->method->name];
     }
     if (isset($exception->exception) && $exception->exception !== null) {
         $exception = $exception->exception;
     }
     if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
         $exception = new HttpException(500, \Yii::t('yii', 'An internal server error occurred.'));
     }
     $array = ['message' => $exception->getMessage(), 'code' => $exception->getCode()];
     if (method_exists($exception, 'getName')) {
         $array['name'] = $exception->getName();
     }
     if ($exception instanceof HttpException) {
         $array['status'] = $exception->statusCode;
     }
     if (YII_DEBUG) {
         $array['type'] = get_class($exception);
         $array['file'] = $exception->getFile();
         $array['line'] = $exception->getLine();
         $array['stack-trace'] = explode("\n", $exception->getTraceAsString());
         if ($exception instanceof \yii\db\Exception) {
             $array['error-info'] = $exception->errorInfo;
         }
         if ($rpc !== null) {
             $array['rpc'] = $rpc;
         }
     }
     if (($prev = $exception->getPrevious()) !== null) {
         $array['previous'] = self::convertExceptionToArray($prev);
     }
     return $array;
 }
コード例 #12
0
 /** @inheritdoc */
 public function __construct($status, $message = null, $code = 0, \Exception $previous = null)
 {
     $this->statusCode = $status;
     if (empty($message)) {
         switch ($status) {
             case 403:
                 $message = \Yii::t('yii', 'You are not allowed to perform this action.');
                 break;
             case 404:
                 $message = \Yii::t('yii', 'No results found.');
                 break;
             default:
                 $message = $this->getName();
                 break;
         }
     }
     parent::__construct($status, $message, $code, $previous);
 }
 public function __construct($extension)
 {
     $message = 'Thumb creation strategy is not implemented for [' . $extension . '] files';
     parent::__construct(400, $message);
 }
コード例 #14
0
ファイル: UserError.php プロジェクト: weiyiyi/base
 public function __construct($message = null, $code = 0, \Exception $previous = null)
 {
     parent::__construct(417, json_encode($message), $code, $previous);
 }
コード例 #15
0
 /**
  * @param string $message The exception message
  * @param int $error The error code returned by the Cloudflare API
  * @param int $httpStatus The HTTP status code of the response
  * @param string $uri The URI queries
  * @param \Exception $previous The previous exception, if any.
  */
 public function __construct($result, $message)
 {
     $this->error = $result;
     parent::__construct('200', $message);
 }
コード例 #16
0
 /**
  * Constructor.
  * @param string $message error message
  * @param integer $code error code
  * @param \Exception $previous The previous exception used for the exception chaining.
  */
 public function __construct($message = 'Invalid csrf token', $code = 0, \Exception $previous = null)
 {
     parent::__construct(403, $message, $code, $previous);
 }
コード例 #17
0
 /**
  * Converts an exception into an array.
  * @param \Exception $exception the exception being converted
  * @return array the array representation of the exception.
  */
 protected function convertExceptionToArray($exception)
 {
     if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
         $exception = new HttpException(500, 'There was an error at the server.');
     }
     $errorCode = $exception->getCode();
     $array = ['code' => $errorCode ? $errorCode : 500, 'message' => $exception->getMessage()];
     if ($exception instanceof LBUserException) {
         $array['data'] = $exception->getErrors();
     }
     return $array;
 }
コード例 #18
0
 /**
  * Constructor.
  * @param integer $status HTTP status code, such as 404, 500, etc.
  * @param string $message error message
  * @param \Exception $previous The previous exception used for the exception chaining.
  */
 public function __construct($status = 503, $message = null, \Exception $previous = null)
 {
     $this->statusCode = $status;
     parent::__construct($status, $message, 0, $previous);
 }
コード例 #19
0
 public function __construct($message = 'FIle does not exists')
 {
     parent::__construct(400, $message);
 }
コード例 #20
0
ファイル: HttpException.php プロジェクト: humandevice/hd-api
 /**
  * Constructor.
  * @param integer $status HTTP status code, such as 404, 500, etc.
  * @param string $message error message
  * @param string $errorUri error uri
  * @param integer $code error code
  * @param \Exception $previous The previous exception used for the exception chaining.
  */
 public function __construct($status, $message = null, $errorUri = null, $code = 0, \Exception $previous = null)
 {
     $this->errorUri = $errorUri;
     parent::__construct($status, $message, $code, $previous);
 }
コード例 #21
0
 public function __construct($message = 'Method is not implemented')
 {
     parent::__construct(501, $message);
 }
コード例 #22
0
 /**
  * Constructor.
  * @param string $message error message
  * @param integer $code error code
  * @param \Exception $previous The previous exception used for the exception chaining.
  */
 public function __construct($message = 'Сервис временно недоступен. Повторите попытку позже.', $code = 0, \Exception $previous = null)
 {
     parent::__construct(200, $message, $code, $previous);
 }
コード例 #23
0
ファイル: ErrorHandler.php プロジェクト: Jaaviieer/PrograWeb
 /**
  * Converts an exception into an array.
  * @param \Exception $exception the exception being converted
  * @return array the array representation of the exception.
  */
 protected function convertExceptionToArray($exception)
 {
     if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
         $exception = new HttpException(500, 'There was an error at the server.');
     }
     $array = ['name' => $exception instanceof Exception || $exception instanceof ErrorException ? $exception->getName() : 'Exception', 'message' => $exception->getMessage(), 'code' => $exception->getCode()];
     if ($exception instanceof HttpException) {
         $array['status'] = $exception->statusCode;
     }
     if (YII_DEBUG) {
         $array['type'] = get_class($exception);
         if (!$exception instanceof UserException) {
             $array['file'] = $exception->getFile();
             $array['line'] = $exception->getLine();
             $array['stack-trace'] = explode("\n", $exception->getTraceAsString());
             if ($exception instanceof \yii\db\Exception) {
                 $array['error-info'] = $exception->errorInfo;
             }
         }
     }
     if (($prev = $exception->getPrevious()) !== null) {
         $array['previous'] = $this->convertExceptionToArray($prev);
     }
     return $array;
 }
コード例 #24
0
 public function __construct($statusCode, $message = null, $code = 0, \Exception $previous = null)
 {
     parent::__construct($statusCode, $message, $code, $previous);
 }
コード例 #25
-1
ファイル: ErrorAction.php プロジェクト: songhongyu/datecenter
 /**
  * Runs the action
  *
  * @return string result content
  */
 public function run()
 {
     if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
         // action has been invoked not from error handler, but by direct route, so we display '404 Not Found'
         $exception = new HttpException(404, Yii::t('yii', 'Page not found.'));
     }
     if ($exception instanceof HttpException) {
         $code = $exception->statusCode;
     } else {
         $code = $exception->getCode();
     }
     if ($exception instanceof Exception) {
         $name = $exception->getName();
     } else {
         $name = $this->defaultName ?: Yii::t('yii', 'Error');
     }
     if ($code) {
         $name .= " (#{$code})";
     }
     if ($exception instanceof UserException) {
         $message = $exception->getMessage();
     } else {
         $message = $this->defaultMessage ?: Yii::t('yii', 'An internal server error occurred.');
     }
     return "{$name}: {$message}";
 }