0

Yii2 shows

SQLSTATE[HY000] [2002] A socket operation was attempted to an unreachable host.

when there is no internet access, how can I make it redirect to a page, says ./index.php?r=site/neterror? I tried to do this in web/index.php by using this code

<?php
use yii\db\Exception;
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
try
{
    (new yii\web\Application($config))->run();  
}
catch(Exception $e) {
    header("Location: ./index.php?r=site/error"); /* Redirect browser */
    exit();
}

but all I get back is The page isn't redirecting properly.

1
  • just change your debug YII_DEBUG environment to 'false' Commented Jun 24, 2016 at 5:19

1 Answer 1

1

Following Database Exception View With different msg shown only when YII_DEBUG is true in Entry Script..

As Defined in Yii ErrorHandler Class to use Own View On UserException you have to false YII_DEBUG

class ErrorHandler extends \yii\base\ErrorHandler{
....
  protected function renderException($exception){
  ..
   $useErrorView = $response->format === Response::FORMAT_HTML && (!YII_DEBUG || $exception instanceof UserException);

    if ($useErrorView && $this->errorAction !== null) {
        $result = Yii::$app->runAction($this->errorAction);
        if ($result instanceof Response) {
            $response = $result;
        } else {
            $response->data = $result;
        }
    } 
  }


}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.