# How do I properly raise an error from within a plugin?

I am successfully preventing some content from being saved under certain conditions, but am having trouble handling the error message to the user.

The actual text does display, but there is additional mention of "Save failed with the followig error:", then nothing follows. It seems redundant since the text displays, but I don't know how to subdue it, or provide the correct information for it so that it doesn't appear incomplete to the user.

public function onContentBeforeSave($context=null,$article=null, $isNew=0) { if($myTestFails){
$errormsg = 'My error message while saving categories...'; JError::raiseWarning(403,$errormsg);
return false;
}
}


Error

My error message while saving categories...

Save failed with the following error:

• What version of Joomla? – Søren Beck Jensen Dec 3 '14 at 8:43
• The version V3.3 - the code was copied from Joomla's content plugin – GDP Dec 3 '14 at 14:34

Because of "return false", you are taken back to the model, so "Save failed with the following error:" message is displayed.

If you don't want to go back to the model, you can redirect user back to your form directly instead of entering the model, then the controller and finally your form view.

But because you go away from the model and the controller, there are something you need to take care of by yourself, for example saving what user has entered into your form to display it in the form automatically after redirect, this helps user not have to fill the form again.

public function onContentBeforeSave($context=null,$article=null, $isNew=0) { if($myTestFails){
$errormsg = 'My error message while saving categories...';$app = JFactory::getApplication();
$app->setUserState(your-form-context-here, your-form-data-here);$app->enqueueMessage($errormsg);$app->redirect(JRoute::_(URL-to-your-form, false));
}
}


You can take a look at the save() functions in libraries/legacy/controller/form.php and libraries/legacy/model/admin.php. There are useful info there.

• I like this, but because it's a plugin event, it's not "my" form, so I have no control over where you referenced any "form" info. – GDP Dec 3 '14 at 14:31

You should probably throw an exception.

JError was deprecated in Joomla 3.0

throw new Exception(JText::_('COM_MYCOMP_ERROR_MESSAGE_NOT_FOUND'), 403);

• You are correct about the method for throwing the error but I still don't think this will actually answer the @GDP's question – Lodder Dec 3 '14 at 9:46
• Joomla uses the method I used in their content plugin, so that was my starting point. Curious about what the impact of an exception from a content plugin might be. (new PC today, so can't try this out for a day or two). – GDP Dec 3 '14 at 14:36