diff --git a/packages/image/src/create-src-for.js b/packages/image/src/create-src-for.js index ef9367978..a40a92bd9 100644 --- a/packages/image/src/create-src-for.js +++ b/packages/image/src/create-src-for.js @@ -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); diff --git a/packages/image/src/crop-rectangle.js b/packages/image/src/crop-rectangle.js index b4c4c0886..6b5253bda 100644 --- a/packages/image/src/crop-rectangle.js +++ b/packages/image/src/crop-rectangle.js @@ -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, diff --git a/services/graphql-server/src/graphql/utils/get-image-dimensions.js b/services/graphql-server/src/graphql/utils/get-image-dimensions.js index de7fc7251..322184ee5 100644 --- a/services/graphql-server/src/graphql/utils/get-image-dimensions.js +++ b/services/graphql-server/src/graphql/utils/get-image-dimensions.js @@ -1,4 +1,5 @@ const fetch = require('node-fetch'); +const newrelic = require('../../newrelic'); /** * Retrieves width/height information from Imgix and sets to @@ -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 }; };