show_error when handling AJAX requests |
[eluser]DanSearle[/eluser]
Hi all, I've written a method in a model that fetches a row from the DB for a given ID, and also compares the current user ID to the row owner. If that ID doesn't match anything, or if the current user ID isn't the owner, then it exits with the show_error() function. In normal operation this never happens - this is to catch misuse. That works fine for normal forms, but now I'm calling the same method as part of handling an AJAX request that returns some JSON feedback. In this situation, the show_error method isn't much use - the caller expects JSON and won't handle the HTML response from show_error. I'd like to be able to send the error back in JSON. Is there a better way to handle the errors here? Should I make my own show_error type function that can react differently for HTML or AJAX requests? Perhaps I shouldn't have used show_error in the model - this is my first CI project, I expect to make a few rookie errors. Thanks, Dan
[eluser]DanSearle[/eluser]
[quote author="DanSearle" date="1294271596"]Hi all, Perhaps I shouldn't have used show_error in the model [/quote] I'll answer my own question ![]() http://ellislab.com/forums/viewthread/59823/ Luipaard makes a good case for using Exceptions in this thread, that seems like a neat way to do it to me. Here's what he said in the other thread: [quote author="Luipaard" date="1188554930"]How about exceptions, if you're using PHP 5 that is. You throw the error in the model class, and you can choose to handle it in the controller, or just let PHP raise it. Model Code: function get_user_id(){ Controller Code: function index() { Exceptions[/quote] |
Welcome Guest, Not a member yet? Register Sign In |