Presently, window.onerror does not report the actual exception object which was thrown from JS - instead an error is emitted if you try to use it:
ERROR 17:28:20,432 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter: runtimeError: message=[Invalid JavaScript value of type com.gargoylesoftware.htmlunit.ScriptException] sourceName=[script in http://localhost:12345/ from (3, 9) to (7, 10)] line=[5] lineSource=[null] lineOffset=[0]
and another if you try to assign properties to it:
ERROR 17:31:07,814 com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter: runtimeError: message=[Java class "com.gargoylesoftware.htmlunit.ScriptException" has no public instance field or method named "property".] sourceName=[script in http://localhost:12345/ from (3, 9) to (8, 10)] line=[5] lineSource=[null] lineOffset=[0]
Proposed patch and tests attached.
Fixed in svn, many thanks for finding this issue and the patch.
Thanks for the quick response.
For others using an older HtmlUnit, this patch is fairly easy to backport if needed, or JavaScriptEngine can be subclassed and handleJavaScriptException overridden with the updated contents.
There is a new snapshot build available.