Skip to content

fix(DOMParser): Align parseFromString errors with specs#454

Merged
karfau merged 17 commits into
masterfrom
fix-error-handling
Jun 14, 2023
Merged

fix(DOMParser): Align parseFromString errors with specs#454
karfau merged 17 commits into
masterfrom
fix-error-handling

Conversation

@karfau

@karfau karfau commented Nov 1, 2022

Copy link
Copy Markdown
Member
  • throw TypeError on invalid mimeType argument, it is now a mandatory argument
  • report fatalError if no documentElement was added, to ensure we never return an invalid/incompletely initialized Document.
  • report fatalError for non string source argument, instead of error, but only for XML
  • refactored MIME_TYPE to not contain functions

fix: Simpler and more robust error handling

There is a new option onError option that can be passed to the DOMParser constructor:
The function that is invoked for every error that occurs during parsing.

If it is not provided, all errors are reported to console.error
and only fatalErrors are thrown as a ParseError, which prevents any further processing.
If onError throws, a ParserError is thrown, which prevents any further processing.

The main reason for joining the different handlers, is that the current error levels make no sense, and all should currently be treated as if they are errors.

The previous errorHandler option has been marked deprecated.
If it is a function and onError is not passed it will be used as onError, but is reported as a warning.
If it is an object a TypeError will be thrown as part of the DOMParser constructor.

BREAKING-CHANGE: It is no longer possible to prevent the fatalError from throwing by setting errorHandler. This ensures that no further processing takes place once a fatalError occurs.
Which is what the XML specifications demand.
This is why so many tests needed to be changed: Now they need to handle the throwing case instead of just collecting messages.

- throw `TypeError` on invalid mimeType argument, it is now a mandatory argument
- report `fatalError` if `Document` was not created or no `documentElement` was added,
  to ensure we never return `undefined`.
- report `fatalError` for non string source argument, instead of `error`, but only for XML
- refactored MIME_TYPE to not contain functions
@karfau karfau force-pushed the fix-error-handling branch from 3f660fa to 960612b Compare November 1, 2022 03:07
By passing `onError` option to `DOMParser` constructor,
it will be called for each kind of error that happens during parsing.
The main reason for joining the different handlers,
is that the current error levels make no sense,
and all should currently be treated as if they are errors.
If it throws the error will be wrapped into a ParseError to stop processing.
If it is not provided, xmldom continues to log to the console like before.
The previous `errorHandler` option has been marked deprecated.
If it is a function it will be used as `onError` if present (and `onError` is not passed), but logged as a warning.
If it is an object a `TypeError` will be thrown.

BREAKING-CHANGE: It is no longer possible to prevent the `fatalError` from throwing by overriding it. This ensures that no further processing takes place once a fatalError occurs.
Which is what the XML specifications demand.
This is why so many tests needed to be changed: Now they need to handle the throwing case instead of just collecting messages.
@karfau karfau added this to the 0.9.0 milestone Jun 9, 2023
karfau added 11 commits June 9, 2023 22:22
# Conflicts:
#	lib/conventions.js
#	test/dom/element.test.js
#	test/dom/serializer.test.js
- drop usage of `Object.assign` since it behaves differently
- all errors are reported to `console.error`
- avoid log output from test
- update/sync jsdoc comments
- `onErrorStopParsing`
- `onWarningStopParsing` (used in typescript example)
- update tons of docs
- update node and typescript versions in CI workflows
- npm script `test:types` requires node 14.17 due to typesript 5.1
@karfau karfau force-pushed the fix-error-handling branch from 3b1c11f to f8c2493 Compare June 12, 2023 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xmldom does not report parsererror (on XML without proper start tag)

1 participant