/** * 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]); } }
/** * 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]); } }
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]); } }
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]); } }
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]; }
/** * 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')); }
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; }
/** * 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; }
/** * 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; }
/** * 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}"; }