diff --git a/backend/src/routes/api/images/imageUtils.ts b/backend/src/routes/api/images/imageUtils.ts index a472667c87..d2f7699229 100644 --- a/backend/src/routes/api/images/imageUtils.ts +++ b/backend/src/routes/api/images/imageUtils.ts @@ -167,8 +167,8 @@ const getTagInfo = (imageStream: ImageStream): ImageTagInfo[] => { const getTagContent = (tag: ImageStreamTag): TagContent => { const content: TagContent = { - software: JSON.parse(tag.annotations[IMAGE_ANNOTATIONS.SOFTWARE] || '[]'), - dependencies: JSON.parse(tag.annotations[IMAGE_ANNOTATIONS.DEPENDENCIES] || '[]'), + software: jsonParsePackage(tag.annotations[IMAGE_ANNOTATIONS.SOFTWARE]), + dependencies: jsonParsePackage(tag.annotations[IMAGE_ANNOTATIONS.DEPENDENCIES]), }; return content; }; @@ -194,16 +194,10 @@ const mapImageStreamToBYONImage = (is: ImageStream): BYONImage => ({ description: is.metadata.annotations['opendatahub.io/notebook-image-desc'], visible: is.metadata.labels['opendatahub.io/notebook-image'] === 'true', error: getBYONImageErrorMessage(is), - packages: - is.spec.tags && - (JSON.parse( - is.spec.tags[0].annotations['opendatahub.io/notebook-python-dependencies'], - ) as BYONImagePackage[]), - software: - is.spec.tags && - (JSON.parse( - is.spec.tags[0].annotations['opendatahub.io/notebook-software'], - ) as BYONImagePackage[]), + packages: jsonParsePackage( + is.spec.tags?.[0]?.annotations?.['opendatahub.io/notebook-python-dependencies'], + ), + software: jsonParsePackage(is.spec.tags?.[0]?.annotations?.['opendatahub.io/notebook-software']), uploaded: is.metadata.creationTimestamp, url: is.metadata.annotations['opendatahub.io/notebook-image-url'], user: is.metadata.annotations['opendatahub.io/notebook-image-creator'], @@ -408,3 +402,11 @@ export const updateImage = async ( } } }; + +const jsonParsePackage = (unparsedPackage: string): BYONImagePackage[] => { + try { + return JSON.parse(unparsedPackage) || []; + } catch { + return []; + } +};