Refactor window.onerror handler