CodeIgniter Forums

Full Version: [Best practice] Ajax and Error notification
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Spir[/eluser]
Hello I just want to know how you guys notify users when an error occurs when using Ajax call.

Let me show an example :

I have some controllers, some models some view and some Ajax. Nothing big. When I do request with Ajax (for my CRUD or various management of data) my Ajax hit a Controller that does a second data check (first check has been done by Ajax itself) if data pass the test it goes to my model. The model process the data. and then return TRUE (best case scenario, controller say TRUE to my Ajax that inform user with a "sucess message".

But what if I handle a bug in model or controller? how should I inform my controller a bug occurs while saving data for instance? I have image to pass an array by ref that would contains error details. so the controller would be able to feed the Ajax with a view that printout the failure detail or maybe give back an array...

The flashdata here could not be used since it's transmitted from one request to another.
Any idea?

El Forum

[eluser]Chris Williams[/eluser]
This system might only work for me, and there may be better ways, but this is what I use.

Firebug will display errors and server responses on Ajax calls. Plus I look at Console.app on my server for error.log entries.

El Forum

[eluser]Spir[/eluser]
Actually I wanted to push the bug up so the user can be notify that an error occurs with a propper message. But also for production env. I mean user can be anyone. So no debug here.

I think what I'm looking for is throw catch exception Smile
I have never set that in PHP (only in Java), it's very usefull.

Doc is dead?
http://ellislab.com/codeigniter/user-gui...tions.html
Code:
/**
* Exceptions Class
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Exceptions
* @author        ExpressionEngine Dev Team
* @link        http://ellislab.com/codeigniter/user-guide/libraries/exceptions.html
*/
class CI_Exceptions {
...

El Forum

[eluser]Spir[/eluser]
So no help about exceptions in CI?

El Forum

[eluser]mvdg27[/eluser]
Not sure if this is what you're after, but the way I'm doing this is through Json-responses. So upon an AJAX call, the controller doesn't just return TRUE or FALSE, but it returns a Json-response, which 1) tells my javascript whether the request was succesfull and 2) returns data from the server. This data could be either some sort of information requested of an error (or an array of errors).

For example in your controller:

Code:
function do_something () {
  if($this->input->post('foo') == 'foo') {
    $output['success'] = true;
    $output['message'] = 'Everything worked allright';
  }
  else {
    $output['success'] = false;
    $output['message'] = 'There was a problem';
  }
  print json_encode($output);
}

Then off course your javascript should read out the Json result and check for the success variable. How exactly this is done, depends on the javascript framework you are using.

Hope that helps.

Michiel

El Forum

[eluser]Spir[/eluser]
Nice and easy Smile (I'm using jQuery)

But what about PHP Exception? So my controller could catch exceptions from the model?
The documentation is off?

http://ellislab.com/codeigniter/user-gui...tions.html

El Forum

[eluser]mvdg27[/eluser]
I've been using exceptions as well, I just posted some code using this in this forum yesterday. See the third post in this topic: http://ellislab.com/forums/viewthread/101904/

I simply use:

Code:
try {
  if(!$this->your_model->do_somthing())
    throw new Exception('your exception');

  $o['success'] = true;
}
catch (Exception $e) {
  $o['success'] = false;
  $o['exception'] = $e->getMessage();
}
print json_encode($o);

However, note that nikefido in the next post has some remarks against using exceptions like this. Furthermore, I haven't explicitly used the codeigniter exceptions class before, so I can't really comment on that.

Michiel

El Forum

[eluser]Spir[/eluser]
Thanks I'll learn some few things there Smile