Skip to content

Commit da399e4

Browse files
Kent C. DoddsMichaelDeBoeypieh
authored
feat(plugin): Log the URL that caused a failure (#102)
* feat: log the URL that caused a failure. The added context is very helpful. * Update index.js * Add tests * Update src/index.js Co-Authored-By: Michal Piechowiak <misiek.piechowiak@gmail.com> * Update index.js * Update index.js * Update tests Co-authored-by: Michaël De Boey <info@michaeldeboey.be> Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent 54ce007 commit da399e4

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://error-site.com

src/__tests__/plugin.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,23 @@ describe('gatsby-remark-embedder', () => {
6969
"
7070
`);
7171
});
72+
73+
test('logs when a transformer errors', async () => {
74+
const errorTransformer = {
75+
getHTML: () => {
76+
throw new Error('An error occurred in ErrorTransformer');
77+
},
78+
shouldTransform: () => true,
79+
};
80+
81+
const markdownAST = getMarkdownASTForFile('ErrorTransformer', true);
82+
83+
await expect(
84+
plugin({ cache, markdownAST }, { customTransformers: [errorTransformer] })
85+
).rejects.toMatchInlineSnapshot(`
86+
[Error: The following error appeared while processing 'https://error-site.com/':
87+
88+
An error occurred in ErrorTransformer]
89+
`);
90+
});
7291
});

src/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,22 @@ export default async (
4848
.filter(({ shouldTransform }) => shouldTransform(urlString))
4949
.forEach(({ getHTML }) => {
5050
transformations.push(async () => {
51-
let html = await cache.get(urlString);
51+
try {
52+
let html = await cache.get(urlString);
5253

53-
if (!html) {
54-
html = await getHTML(urlString);
55-
await cache.set(urlString, html);
56-
}
54+
if (!html) {
55+
html = await getHTML(urlString);
56+
await cache.set(urlString, html);
57+
}
58+
59+
node.type = `html`;
60+
node.value = html;
61+
node.children = undefined;
62+
} catch (error) {
63+
error.message = `The following error appeared while processing '${urlString}':\n\n${error.message}`;
5764

58-
node.type = `html`;
59-
node.value = html;
60-
node.children = undefined;
65+
throw error;
66+
}
6167
});
6268
});
6369
});

0 commit comments

Comments
 (0)