dark_gf
dark_gf

Reputation: 726

Custom error handler for any exception

Problem is that i do all according this topic http://www.yiiframework.com/doc/guide/1.1/en/topics.error, but when in my controller i raise exception( throw new Exception("error"); ), yii don't route to my custom error controller and use system default. How to handle such exceptions?

Upvotes: 2

Views: 2491

Answers (2)

hemc4
hemc4

Reputation: 1633

in case of exception from mysql run your queries as

$insertCommand = $model->getCommandBuilder()->createSqlCommand($sql,$baseParams+$relParams);
try {
        $insertCommand->execute();
} catch(CDbException $e) {
        // Here's a way to get to the error code for the statement in question.
        // These codes are standardized ANSI SQL "SQLSTATE" error codes.
        $sqlErrorCode = $insertCommand->pdoStatement->errorCode();
        // And to get to the class part of it, simple grab the two first characters.
        // The class should be the same regardless of DB vendor, while the rest of the code can differ.
        // For example one particular error was reported by PostgreSQL as 23505 but MySQL only said 23000.
        $sqlErrorCodeClass = substr($sqlErrorCode, 0, 2);
}

Upvotes: 1

jborch
jborch

Reputation: 1136

You have to use one of Yii's own Exception classes: CException, CDbException or CHttpException. From the link you gave: http://www.yiiframework.com/doc/guide/1.1/en/topics.error#raising-exceptions

// if post ID is invalid
throw new CHttpException(404,'The specified post cannot be found.');

Upvotes: 3

Related Questions