views:

143

answers:

2

I've created a custom datasource which fetches data from a web api, and I'm now looking at implementing error handling.

In the datasource, I'm calling $model->onError(). In the model, I've created the onError method, and I can access error details with $this->getDataSource()->error;

However I can't redirect or set a flash message because that can only take place in the controller, so what should I be doing here to communicate the error to the user?

A: 

I would compile all the errors in the model into a set of errors, perhaps in an array. Then I'd set it as a variable in the Model.

Thus in my controller I can then do $this->Model->getErrors() or similar to read and return the value set in the model.

Then in my controller's beforeFilter() I'd check to see if there were any errors, and if there were, push them into the flash. $this->Session->setFlash($this->Model->getErrors(),'default',array('class'=>'error-message'));

DavidYell
A: 

Are the errors relevant to fields in your model? If so, use $this->invalidate($field, $message) in Model::onError()

neilcrookes
Thanks.I've added $this->invalidate(0,$this->getDataSource()->error); into model onError method.To access it from the controller I'm doing:if (!$this->Model->validates()) $this->flash($this->Model->validationErrors[0],redirect location..);
Richard