Don’t use alert() on AJAX webservice errors!
Even if you want the user to be notified of the fact that an AJAX webservice call has errored out, don’t use an alert() to do it.
Why? Lots of reasons… here are a few:
- If you have one of more calls that error out before the user can dismiss the popup, this is just weird. In IE, you’ll get successive popups, in Firefox, the alerts will stack up over top of each other. Nasty either way, I think.
- If you have a long-running call (and this in an of itself is nasty, but it can happen) and the user browses to another page, you can get into a situation where those error messages popup simply because the calls have been aborted.
- It’s annoying to get notified of these kinds of things unless the user really needs to know. This is up to the designer or architect to figure out, and if you are building a system where you aren’t sure whether the user should know about a given error, this is something that should already have been decided and you need to step back and figure that out.
What should you do to handle these errors?
- If the error only affects a certain portion of the screen, usually you’ll want to disable the affected area and show some kind of error indicator there.
- If you NEED to have a popup, use an HTML overlay or something non-blocking that will allow the user to navigate away without being stopped to click “OK”.
- Always put a try/catch in your web methods with a finally that returns an error specific for the browser to interpret.
- There’s no need to send a server stack trace down to the user.
- If you do any automated error logging, use that in the web method itself, not from Javascript.
These are just a few things to keep in mind, and this is be no means an exhaustive list of caveats and workarounds.
Feel free to suggest others.
more later – joel.