Yii Framework Forum: Exception handling & Application logging - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Exception handling & Application logging Rate Topic: -----

#1 User is offline   Yohan Hirimuthugoda 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 11-January 11
  • Location:Sri Lanka

Posted 10 July 2012 - 03:57 AM

Hi,

I am using below code to capture "SQLSTATE 23503: Foreign key violation" error code to display friendly message to end user. Displaying friendly message & everything working fine except it will generate entry under application.log of Yii. It's:

2012/07/10 19:07:05 [error] [system.db.CDbCommand] CDbCommand::execute() failed: SQLSTATE[23503]: Foreign key violation: 7 ERROR:  update or delete on table "posts" violates foreign key constraint "fk_comments_posts" on table "comments"
DETAIL:  Key (id)=(3041) is still referenced from table "comments".. The SQL statement executed was: DELETE FROM "posts" WHERE "posts"."id"='3041'.


My application code:

try {
   if ($post->delete()) {
      Yii::app()->user->setFlash('success', Yii::t('post', 'The Post has been successfully deleted.'));
      $success = true;
   } else
      Yii::app()->user->setFlash('error',  Yii::t('post', $post->getLastError()));
} catch (Exception $e) {
   if ($e->getCode() == 23503) {
      Yii::app()->user->setFlash('error', Yii::t('post', 'The Post record cannot be deleted. Post already in use.'));
      $success = true;      
   } else
      Yii::app()->user->setFlash('error', Yii::t('post', $e->getMessage()));
}

if ($success)
   echo 'success';
else
   echo 'failed';


Could anybody point me what is the mistake I am doing or is this the default behavior of PHP exception handling process? Because I thought if I handling the exception it won't produce any entries under logs.

Thank you,
Yohan
0

#2 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 10 July 2012 - 04:02 AM

Hi,
You have choice: you should modify database to have a cascading delete from table posts to table comments, or authorize delete of posts after checking that there is no more related comments.
0

#3 User is offline   Yohan Hirimuthugoda 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 11-January 11
  • Location:Sri Lanka

Posted 10 July 2012 - 04:11 AM

@ragua: Thank you for trying to help me. But I need explanation why Yii log error under application.log though I have handle the exception?
0

#4 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 10 July 2012 - 04:19 AM

If i understand correctly your question, this is not an exception: from yii, your code is correct and is sent to the database, which meet the problem. That's why one of the solution is to modify database.
0

#5 User is offline   Yohan Hirimuthugoda 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 26
  • Joined: 11-January 11
  • Location:Sri Lanka

Posted 10 July 2012 - 06:23 AM

I think I have found the reason for this. Check below explanation:

Quote

4. Message Logging
A message of level error will always be logged when an error occurs. If the error is caused by a PHP warning or notice, the message will be logged with category php; if the error is caused by an uncaught exception, the category would be exception.ExceptionClassName (for CHttpException its statusCode will also be appended to the category). One can thus exploit the logging feature to monitor errors happened during application execution.


It's under this link: Error Handling

No matter we are handling or not the exception Yii always logged the error to application.log when an error occurs.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users