Wysmedia.com

Icon

~ We make IT easy for you ~

Trapping Server Error

Sometimes we found that our server are not behave normal, like there are bugs or server produce runtime exception (or error 404 etc). Those exception are not returned by ajax. So it will keep hidden and seems nothing happened. But if you have firebug installed, you can found that there are errors happened from your server.

So here is how you can trap all errors and displayed on your screen (without using firebug).

This example works on grails main layout (but you can use this tips on any programming language, just put this code on main layout). My code using Jquery and JQalert to display the error

1
2
3
4
5
6
7
$(document).ready(function(){
                $(document).ajaxError(function(event, request, setting){
                    var re = /<br\/>|<br>/g; // for replacing all <br/> with empty string in error 404
                    var txt = request.responseText.replace(re,"");
                    jqalert(txt,"Server Error");
                });
});