Skip to content

Commit c4416e3

Browse files
committed
Add improved error messages
1 parent 87bc514 commit c4416e3

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

Diff for: lib/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,22 @@ export function fromXml(value) {
6565
preserveXmlDeclaration: true
6666
})
6767
} catch (error_) {
68-
const error = /** @type {XmlError} */ (error_)
69-
const point = loc.toPoint(error.pos)
70-
throw new VFileMessage(error.message, point, 'xast-util-from-xml:error')
68+
const cause = /** @type {XmlError} */ (error_)
69+
const place = loc.toPoint(cause.pos)
70+
const message = new VFileMessage(
71+
'Could not parse XML with `@rgrove/parse-xml`',
72+
{
73+
cause,
74+
place,
75+
ruleId: 'error',
76+
source: 'xast-util-from-xml'
77+
}
78+
)
79+
80+
message.fatal = true
81+
message.url = 'https://github.com/syntax-tree/xast-util-from-xml#throws'
82+
83+
throw message
7184
}
7285

7386
const state = {location: loc}

Diff for: readme.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,21 @@ Parse a string of XML to a xast tree.
136136

137137
##### Parameters
138138

139-
* `value` (`string` or `Buffer` in UTF-8).
139+
* `value` (`Uint8Array` in UTF-8 or `string`).
140140
— serialized XML
141141

142142
##### Returns
143143

144144
xast root ([`Root`][root]).
145145

146+
###### Throws
147+
148+
When the XML cannot be parsed with `@rgrove/parse-xml`, a
149+
[`VFileMessage`][vfile-message] is thrown.
150+
151+
This can for example happen when passing archaic or unsafe XML (such as
152+
entities), or just otherwise invalid XML such as missnested tags.
153+
146154
## Types
147155

148156
This package is fully typed with [TypeScript][].
@@ -234,6 +242,8 @@ abide by its terms.
234242

235243
[root]: https://github.com/syntax-tree/xast#root
236244

245+
[vfile-message]: https://github.com/vfile/vfile-message
246+
237247
[parse-xml]: https://github.com/rgrove/parse-xml
238248

239249
[xast-util-to-xml]: https://github.com/syntax-tree/xast-util-to-xml

Diff for: test/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test('fromXml', async function (t) {
2121
fromXml('<root unquoted=attribute>')
2222
assert.fail()
2323
} catch (error) {
24-
assert.match(String(error), /^1:16: Attribute value expected/)
24+
assert.match(String(error), /^1:16: Could not parse XML/)
2525
}
2626
})
2727

@@ -30,7 +30,7 @@ test('fromXml', async function (t) {
3030
fromXml('<!ENTITY>')
3131
assert.fail()
3232
} catch (error) {
33-
assert.match(String(error), /^1:1: Root element is missing or invalid/)
33+
assert.match(String(error), /^1:1: Could not parse XML/)
3434
}
3535
})
3636

@@ -39,7 +39,7 @@ test('fromXml', async function (t) {
3939
fromXml('<root>&foo;</root>')
4040
assert.fail()
4141
} catch (error) {
42-
assert.match(String(error), /^1:7: Named entity isn't defined/)
42+
assert.match(String(error), /^1:7: Could not parse XML/)
4343
}
4444
})
4545

@@ -48,7 +48,7 @@ test('fromXml', async function (t) {
4848
fromXml('<root>&copy;</root>')
4949
assert.fail()
5050
} catch (error) {
51-
assert.match(String(error), /^1:7: Named entity isn't defined/)
51+
assert.match(String(error), /^1:7: Could not parse XML/)
5252
}
5353
})
5454

@@ -57,7 +57,7 @@ test('fromXml', async function (t) {
5757
fromXml('<root><a><b><c/></a></b></root>')
5858
assert.fail()
5959
} catch (error) {
60-
assert.match(String(error), /^1:17: Missing end tag for element/)
60+
assert.match(String(error), /^1:17: Could not parse XML/)
6161
}
6262
})
6363
})

0 commit comments

Comments
 (0)