WordPress/openverse-api

`piexif.dump` errors are not safely handled

sarayourfriend opened this issue · 2 comments

Sentry link

https://sentry.io/share/issue/a80d52de7f89436586ed0250cd0a32d2/

Description

The call to piexif.dump should be wrapped in a try/except to prevent these errors in the watermark endpoint.

Reproduction

Visit https://api-dev.openverse.engineering/v1/images/a913fde1-d524-4059-bd4f-9bd687578cc3/watermark/ to see an example of this failure.

Closing this in favor of #827, as both Sentry errors are manifestations of the same issue. I'll also merge the Sentry issues together.

We're not catching all the exceptions on this code block so reopening.

try:
response = requests.get(url, headers=HEADERS)
img_bytes = BytesIO(response.content)
img = Image.open(img_bytes)
# Preserve EXIF metadata
if "exif" in img.info:
exif = piexif.load(img.info["exif"])
else:
exif = None
return img, exif
except requests.exceptions.RequestException as e:
capture_exception(e)
logger.error(f"Error loading image data: {e}")
return None, None