Skip to content

Commit

Permalink
docx: improve error message when fetching images, include url to imag…
Browse files Browse the repository at this point in the history
…e in the mesage

fix #1132
  • Loading branch information
bjrmatos committed May 2, 2024
1 parent 7621f91 commit 9ec3ad8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/jsreport-docx/lib/imageUtils.js
Expand Up @@ -61,8 +61,17 @@ module.exports.resolveImageSrc = async function resolveImageSrc (reporter, src)
}
}
} catch (error) {
error.imageSource = imageSource
throw error
let wrappedError = error

if (imageSource === 'remote' && wrappedError?.config?.url != null) {
wrappedError = reporter.createError(`Unable to fetch remote image at ${wrappedError.config.url}`, {
original: error
})
}

wrappedError.imageSource = imageSource

throw wrappedError
}

return { imageSource, imageContent, imageExtension }
Expand Down
32 changes: 32 additions & 0 deletions packages/jsreport-docx/test/imageTest.js
Expand Up @@ -1120,6 +1120,38 @@ describe('docx image', () => {
fs.writeFileSync(outputPath, result.content)
})

it('image error message should include url to image when src points to remote image', async () => {
const format = 'png'
const url = `https://some-server.com/some-image.${format}`

reporter.tests.beforeRenderEval((req, res, { require }) => {
require('nock')('https://some-server.com')
.get(`/some-image.${req.data.imageFormat}`)
.reply(500)
})

try {
await reporter.render({
template: {
engine: 'handlebars',
recipe: 'docx',
docx: {
templateAsset: {
content: fs.readFileSync(path.join(docxDirPath, 'image.docx'))
}
}
},
data: {
src: url
}
})

throw new Error('should not reach here')
} catch (error) {
error.message.should.containEql(`unable to fetch remote image at ${url}`)
}
})

it('image error message when no src provided', async () => {
return reporter
.render({
Expand Down

0 comments on commit 9ec3ad8

Please sign in to comment.