sane-domparser-error is a JavaScript utility library designed to standardize error handling for DOMParser and XMLHttpRequest (when `responseType = 'document'`) in web environments. Typically, when an XML document cannot be parsed, these native APIs do not throw an exception but instead return a document containing a `<parsererror>` element. This library abstracts away the inconsistencies of the `<parsererror>` document across different browsers, providing a consistent mechanism to detect and report parsing failures as standard JavaScript `Error` objects. The package is currently at version 0.2.0, with its last update over a decade ago, indicating a maintenance status with a very infrequent release cadence. Its primary differentiator is simplifying robust XML parsing error detection, which is complex due to browser-specific `parsererror` document structures and namespaces.
npm install sane-domparser-errorVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a malformed XML string using DOMParser and then utilize sane-domparser-error to catch and standardize the resulting parsing error into a throwable JavaScript Error object, contrasting it with successful parsing.
Always use `saneError.failOnParseError(doc)` after parsing with DOMParser, or manually check for `doc.getElementsByTagName('parsererror').length > 0`.Rely on `sane-domparser-error` to handle browser inconsistencies in parsing error detection, or implement robust cross-browser checks manually using `namespaceURI` and tag names.
For Node.js environments, use a DOM implementation like `jsdom` or `@xmldom/xmldom` to polyfill `DOMParser` before attempting to use `sane-domparser-error`. Example: `const { JSDOM } = require('jsdom'); global.DOMParser = new JSDOM().window.DOMParser;`Install and configure a DOM polyfill such as `jsdom` or `@xmldom/xmldom` in your Node.js application to provide a global `DOMParser` implementation. `const { JSDOM } = require('jsdom'); global.DOMParser = new JSDOM().window.DOMParser;`After parsing, always pass the resulting document to `saneError.failOnParseError(doc);` within a `try...catch` block to ensure that parsing errors are converted into throwable JavaScript errors.
No dependency data recorded yet.