Skip to content

Commit

Permalink
Merge pull request #616 from bugsnag/fix/inline-script-line-numbers
Browse files Browse the repository at this point in the history
fix(plugin-inline-script-content): Ensure line numbers are correct when error is at line 1/2/3
  • Loading branch information
bengourley authored Sep 6, 2019
2 parents 86b60fa + 1c9fe96 commit ec57654
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ module.exports = {
const htmlLines = [ '<!-- DOC START -->' ].concat(html.split('\n'))
const zeroBasedLine = lineNumber - 1
const start = Math.max(zeroBasedLine - 3, 0)
const end = Math.min(zeroBasedLine + 3, htmlLines.length - 1)
const end = Math.min(zeroBasedLine + 3, htmlLines.length)
return reduce(htmlLines.slice(start, end), (accum, line, i) => {
accum[i + lineNumber - 3] = line.length <= MAX_LINE_LENGTH ? line : line.substr(0, MAX_LINE_LENGTH)
accum[start + 1 + i] = line.length <= MAX_LINE_LENGTH ? line : line.substr(0, MAX_LINE_LENGTH)
return accum
}, {})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,36 @@ Lorem ipsum dolor sit amet.
window.removeEventListener('click', myfun)
expect(spy).toHaveBeenCalledTimes(2)
})

it('gets the correct line numbers for errors at the start of the document', () => {
const scriptContent = `throw new Error('oh')\nconsole.log('next')`
const document = {
scripts: [ { innerHTML: scriptContent } ],
currentScript: { innerHTML: scriptContent },
documentElement: {
outerHTML: `<script>${scriptContent}</script>`
}
}
const window = { location: { href: 'https://app.bugsnag.com/errors' } }

const client = new Client(VALID_NOTIFIER)
const payloads = []
client.setOptions({ apiKey: 'API_KEY_YEAH' })
client.configure()
client.use(plugin, document, window)

expect(client.config.beforeSend.length).toBe(1)
client.delivery(client => ({ sendReport: (payload) => payloads.push(payload) }))
client.notify(new Report('Error', 'oh', [
{ fileName: window.location.href, lineNumber: 1 }
]))
expect(payloads.length).toEqual(1)
expect(payloads[0].events[0].stacktrace[0].code).toEqual({
1: '<!-- DOC START -->',
2: '<script>throw new Error(\'oh\')',
3: 'console.log(\'next\')</script>'
})
expect(payloads[0].events[0].metaData.script).toBeDefined()
expect(payloads[0].events[0].metaData.script.content).toEqual(scriptContent)
})
})

0 comments on commit ec57654

Please sign in to comment.