Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Do not throw fatal error when images are missing/invalid
Browse files Browse the repository at this point in the history
Prevents the GraphQL API from fatally erroring when an image is not found
  • Loading branch information
zarathustra323 committed Apr 6, 2022
1 parent 91c3df8 commit e7601b4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
3 changes: 3 additions & 0 deletions packages/image/src/create-src-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module.exports = (host, image, options, defaultOptions) => {
fileName,
cropDimensions,
} = image;
if (!fileName || !filePath) {
return buildImgixUrl(`https://${host}/asset-is-missing-file-name-or-path`, options, defaultOptions);
}
const path = cropDimensions && cropDimensions.aspectRatio ? `${filePath}/${cropDimensions.aspectRatio}` : filePath;
const src = `https://${host}/${path}/${fileName}`;
return buildImgixUrl(src, options, defaultOptions);
Expand Down
2 changes: 1 addition & 1 deletion packages/image/src/crop-rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CropRectangle {
* the crop area.
*/
module.exports = ({ width, height, cropDimensions }) => {
if (!cropDimensions) {
if (!cropDimensions || !width || !height) {
return new CropRectangle({
x: 0,
y: 0,
Expand Down
20 changes: 13 additions & 7 deletions services/graphql-server/src/graphql/utils/get-image-dimensions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fetch = require('node-fetch');
const newrelic = require('../../newrelic');

/**
* Retrieves width/height information from Imgix and sets to
Expand All @@ -18,12 +19,17 @@ module.exports = async ({
} = image;
if (width && height) return { width, height };
const url = `https://${host}/${filePath}/${fileName}?fm=json`;
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Image at ${filePath}/${fileName} ${res.statusText} ID: ${image._id}`);
try {
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Image at ${filePath}/${fileName} ${res.statusText} ID: ${image._id}`);
}
const { PixelWidth, PixelHeight } = await res.json();
const $set = { width: PixelWidth, height: PixelHeight };
await basedb.updateOne('platform.Asset', { _id: image._id }, { $set });
return { width: PixelWidth, height: PixelHeight };
} catch (e) {
newrelic.noticeError(e);
return { width: 0, height: 0 };
}
const { PixelWidth, PixelHeight } = await res.json();
const $set = { width: PixelWidth, height: PixelHeight };
await basedb.updateOne('platform.Asset', { _id: image._id }, { $set });
return { width: PixelWidth, height: PixelHeight };
};

0 comments on commit e7601b4

Please sign in to comment.