chief
chief

Reputation: 23

return statement

after_photo_post_process :post_process_photo

 def post_process_photo
  img = EXIFR::JPEG.new(photo.queued_for_write[:original].path)  # error on this line
  return unless img

  self.width = img.width
  self.height = img.height
  self.model = img.model
end

I am using a ruby gem called EXIFR that extracts the EXIF data from JPEG files. The EXIF data is simply technical data about the picture. So my code works fine when I upload a JPEG, however any other image type causes it to break.

EXIFR::MalformedJPEG in ImagesController#create

no start of image marker found

I assumed that the return statement would allow this to work if nothing gets assigned to the img variable, but that looks like it is not the case.

Upvotes: 2

Views: 1181

Answers (1)

rwilliams
rwilliams

Reputation: 21497

You could rescue the error and return something else.

def post_process_photo
  begin 
    img = EXIFR::JPEG.new(photo.queued_for_write[:original].path)  # error on this line
    self.width = img.width
    self.height = img.height
    self.model = img.model
  rescue EXIFR::MalformedJPEG
    return nil
  end
end

Upvotes: 1

Related Questions