Skip to content

Commit

Permalink
Add error handling for metadata server
Browse files Browse the repository at this point in the history
  • Loading branch information
RRethy committed Jul 13, 2021
1 parent ae90855 commit dfd19e1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion activestorage/lib/active_storage/service/gcs_service.rb
Expand Up @@ -8,6 +8,9 @@ module ActiveStorage
# Wraps the Google Cloud Storage as an Active Storage service. See ActiveStorage::Service for the generic API
# documentation that applies to all services.
class Service::GCSService < Service
class MetadataServerError < ActiveStorage::Error; end
class MetadataServerNotFoundError < ActiveStorage::Error; end

def initialize(public: false, **config)
@config = config
@public = public
Expand Down Expand Up @@ -190,7 +193,17 @@ def issuer
request = Net::HTTP::Get.new(uri.request_uri)
request["Metadata-Flavor"] = "Google"

http.request(request).body
begin
response = http.request(request)
rescue SocketError
raise MetadataServerNotFoundError
end

if response.is_a?(Net::HTTPSuccess)
response.body
else
raise MetadataServerError
end
end
end

Expand Down

0 comments on commit dfd19e1

Please sign in to comment.