Report exceptions to JS when the DOM implementation fails.

As a bonus, adds some debugging statements.
Add test.
Merge pull request #1304 from Ms2ger/exceptions

Report exceptions to JS when the DOM implementation fails.