Converted to jQuery to write HTML to the DOM and error handling now works.