Skip to content

Commit

Permalink
add more specific error for twitter (mostly jsut showing original url…
Browse files Browse the repository at this point in the history
… in addition to oembed one)
  • Loading branch information
pieh committed Mar 18, 2020
1 parent 68baffa commit dad4868
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/__tests__/transformers/Twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,26 @@ test('Plugin can transform Twitter links', async () => {
"
`);
});

test(`Throws meaningful error if OEmbed data can't be fetched`, async () => {
fetchMock.mockResolvedValueOnce(
new Response(
JSON.stringify({
error: 'Sorry, you are not authorized to see this status.',
request:
'/oembed?url=https://twitter.com/mattconvente/status/1099706762897342465&dnt=true&omit_script=true',
}),
{
status: 403,
}
)
);

await expect(
getHTML('https://twitter.com/mattconvente/status/1099706762897342465')
).rejects.toMatchInlineSnapshot(`
[Error: Failed to fetch OEmbed Data for https://twitter.com/mattconvente/status/1099706762897342465.
Original reason: Request to https://publish.twitter.com/oembed?url=https://twitter.com/mattconvente/status/1099706762897342465&dnt=true&omit_script=true returned non-OK status (403)]
`);
});
21 changes: 14 additions & 7 deletions src/transformers/Twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@ export const getHTML = url => {

return fetchOEmbedData(
`https://publish.twitter.com/oembed?url=${twitterUrl}&dnt=true&omit_script=true`
).then(({ html }) =>
[html]
.map(s => s.replace(/\?ref_src=twsrc.*?fw/g, ''))
.map(s => s.replace(/<br>/g, '<br />'))
.join('')
.trim()
);
)
.then(({ html }) =>
[html]
.map(s => s.replace(/\?ref_src=twsrc.*?fw/g, ''))
.map(s => s.replace(/<br>/g, '<br />'))
.join('')
.trim()
)
.catch(err => {
// rethrow error with finer details
throw new Error(
`Failed to fetch OEmbed Data for ${url}.\n\nOriginal reason: ${err.message}`
);
});
};

0 comments on commit dad4868

Please sign in to comment.