Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fog (AWS S3) file read throws an inappropriate error if file doesn't exist #2524

Closed
AnrichVS opened this issue Dec 10, 2020 · 0 comments
Closed

Comments

@AnrichVS
Copy link

When CarrierWave::Storage::Fog::File#read is called on a file that doesn't exist (specifically on AWS S3 in this case), the following error is thrown:

NoMethodError: undefined method `body' for nil:NilClass

This occurs here - carrierwave-2.1.0/lib/carrierwave/storage/fog.rb:298

297        def read
298          file_body = file.body
299
300          return if file_body.nil?

This is because fileis nil. This is to be expected if the remote file doesn't exist, as per the Fog::AWS::Storage::Files#head method in fog-aws-3.7.0/lib/fog/aws/models/storage/files.rb:99

        def head(key, options = {})
          requires :directory
          data = service.head_object(directory.key, key, options)
          normalize_headers(data)
          file_data = data.headers.merge({
            :key => key
          })
          new(file_data)
        rescue Excon::Errors::NotFound
          nil
        end

The rescue triggers if the remote file doesn't exist, which returns nil.

The CarrierWave::Storage::Fog::File#read method should probably be:

297        def read
298          file_body = file&.body
299
300          return if file_body.nil?
@mshibuya mshibuya added this to the Release v3.0.0 milestone Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants