Displaying Errors

Hello,

I have this error in several places in my application.

How do I catch it and display an alert message for user?

This is an "integrity constraint violation"… something like you are trying to insert a PK that already exist… or you are trying to insert a FK that does not exist in the related table…

All this kind of errors you should catch with the validation before you even try to save that data…

This error is generated when I want to delete a line that is used in other tables.

I did a manual test and it works, but as Yii handles exceptions I thought about doing a test on the error code and then display it in an alert.

how can I do? knowing that this test must be unique across the application

I am not sure about your requirement, but why dont you use a try… catch?

I normally do that on my CMSs and then on error do something like this:




.

.

.

}catch(Exception $e){

	// this is only for initiated transactions of course

	$transaction->rollBack();

	$errors = $model->getErrors();

	

        $msg = '';

				

	if(count($errors)){

		foreach($errors as $field=>$error){

			$msg .= "<strong>$field</strong>: $error <br/>";

		}

	}else {

	       $msg = 'Ha ocurrido un error inesperado:<br/><strong>' . $e->getMessage() . '</strong><br/>Operaci&oacute;n cancelada!';

	}

	// HERE I RENDER the error messages

	$this->renderPartial('application.views.global._message',array('type'=>'error','message'=>$msg));

}

			

  Yii::app()->end();

			



Would it be better to write a global function for that matter… Anyway, just dropping ideas