[dynamicxml] wrap xpath errors (#3955)

Closes #3796
This commit is contained in:
chris48s
2019-09-07 18:02:27 +01:00
committed by Paul Melnikow
parent 58a5239e18
commit 873172522f
2 changed files with 38 additions and 4 deletions

View File

@@ -4,7 +4,7 @@ const { DOMParser } = require('xmldom')
const xpath = require('xpath')
const { renderDynamicBadge, errorMessages } = require('../dynamic-common')
const { createRoute } = require('./dynamic-helpers')
const { BaseService, InvalidResponse } = require('..')
const { BaseService, InvalidResponse, InvalidParameter } = require('..')
// This service extends BaseService because it uses a different XML parser
// than BaseXmlService which can be used with xpath.
@@ -41,9 +41,15 @@ module.exports = class DynamicXml extends BaseService {
const parsed = new DOMParser().parseFromString(buffer)
const values = xpath
.select(pathExpression, parsed)
.map((node, i) => (pathIsAttr ? node.value : node.firstChild.data))
let values
try {
values = xpath.select(pathExpression, parsed)
} catch (e) {
throw new InvalidParameter({ prettyMessage: e.message })
}
values = values.map((node, i) =>
pathIsAttr ? node.value : node.firstChild.data
)
if (!values.length) {
throw new InvalidResponse({ prettyMessage: 'no result' })

View File

@@ -123,6 +123,34 @@ t.create('query doesnt exist (attribute)')
color: 'lightgrey',
})
t.create('Cannot resolve QName')
.get(
`.json?${queryString.stringify({
url: exampleUrl,
query: '//a:si',
})}`
)
.intercept(withExampleXml)
.expectBadge({
label: 'custom badge',
message: 'Cannot resolve QName a',
color: 'red',
})
t.create('XPath parse error')
.get(
`.json?${queryString.stringify({
url: exampleUrl,
query: '//a[contains(@href, "foo"]',
})}`
)
.intercept(withExampleXml)
.expectBadge({
label: 'custom badge',
message: 'XPath parse error',
color: 'red',
})
t.create('XML from url | invalid url')
.get(
'.json?url=https://github.com/badges/shields/raw/master/notafile.xml&query=//version'