Skip to content

Commit

Permalink
feat(Reactions): add proper error handling to getImageDimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Feb 28, 2023
1 parent bd250f1 commit 7bc4242
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/components/Reactions/SpriteImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const SpriteImage = ({
const [[spriteWidth, spriteHeight], setSpriteDimensions] = useState([0, 0]);

useEffect(() => {
getImageDimensions(spriteUrl).then(setSpriteDimensions);
getImageDimensions(spriteUrl).then(setSpriteDimensions).catch(console.error);
}, [spriteUrl]);

const [x, y] = position;
Expand All @@ -42,6 +42,7 @@ export const SpriteImage = ({

return (
<div
data-testid='sprite-image'
style={{
backgroundImage: `url('${spriteUrl}')`,
backgroundPosition: `${x * (100 / (columns - 1))}% ${y * (100 / (rows - 1))}%`,
Expand Down
4 changes: 3 additions & 1 deletion src/components/Reactions/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export const getImageDimensions = (source: string) =>
{ once: true },
);

image.addEventListener('error', reject, { once: true });
image.addEventListener('error', () => reject(`Couldn't load image from ${source}`), {
once: true,
});

image.src = source;
});

0 comments on commit 7bc4242

Please sign in to comment.