The comment doesn't match the code. You're picking one of the unfiltered, possbily invalid sizes.

The only way I can see the sort().shift() code failing is if there are no valid sizes. and in that case, picking previewSizes[0] probably makes sense.  How about testing validPreviews.length and then sort or use the first preview size based on that?