Improving error handling for failed XMLHttpRequest