Skip to content

Commit c6d0575

Browse files
manny-jimenezManny Jimenez
andauthored
Adding parsing error handling and improving error messages (#282)
* Adding parsing erorr handling and improving error messages on missing or nil tokens field * Adding in quotes for the string * Responding to comments * running rubocop * Adding in line that I removed * Changing helpful messages to new errors and updating tests * Fix broken tests and adding in a new one Co-authored-by: Manny Jimenez <mannyjimenez@google.com>
1 parent d999708 commit c6d0575

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

lib/fastlane/plugin/firebase_app_distribution/helper/firebase_app_distribution_auth_client.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,27 @@ def fetch_auth_token(google_service_path, firebase_cli_token, debug = false)
4848
private
4949

5050
def refresh_token_from_firebase_tools
51-
if ENV["XDG_CONFIG_HOME"].nil? || ENV["XDG_CONFIG_HOME"].empty?
52-
config_path = File.expand_path(".config/configstore/firebase-tools.json", "~")
53-
else
54-
config_path = File.expand_path("configstore/firebase-tools.json", ENV["XDG_CONFIG_HOME"])
55-
end
56-
51+
config_path = format_config_path
5752
if File.exist?(config_path)
5853
begin
59-
refresh_token = JSON.parse(File.read(config_path))['tokens']['refresh_token']
60-
refresh_token unless refresh_token.nil? || refresh_token.empty?
61-
# TODO: Catch parser errors, improve error handling here
62-
# Returns nil when there is an empty "tokens" field in the firebase-tools json
63-
rescue NoMethodError
54+
firebase_tools_tokens = JSON.parse(File.read(config_path))['tokens']
55+
if firebase_tools_tokens.nil?
56+
UI.user_error!(ErrorMessage::EMPTY_TOKENS_FIELD)
57+
return
58+
end
59+
refresh_token = firebase_tools_tokens['refresh_token']
60+
rescue JSON::ParserError
61+
UI.user_error!(ErrorMessage::PARSE_FIREBASE_TOOLS_JSON_ERROR)
6462
end
63+
refresh_token unless refresh_token.nil? || refresh_token.empty?
64+
end
65+
end
66+
67+
def format_config_path
68+
if ENV["XDG_CONFIG_HOME"].nil? || ENV["XDG_CONFIG_HOME"].empty?
69+
File.expand_path(".config/configstore/firebase-tools.json", "~")
70+
else
71+
File.expand_path("configstore/firebase-tools.json", ENV["XDG_CONFIG_HOME"])
6572
end
6673
end
6774

lib/fastlane/plugin/firebase_app_distribution/helper/firebase_app_distribution_error_message.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ErrorMessage
33
MISSING_APP_ID = "Missing app id. Please check that the app parameter is set and try again"
44
SERVICE_CREDENTIALS_NOT_FOUND = "Service credentials file does not exist. Please check the service credentials path and try again"
55
PARSE_SERVICE_CREDENTIALS_ERROR = "Failed to extract service account information from the service credentials file"
6+
PARSE_FIREBASE_TOOLS_JSON_ERROR = "Encountered error parsing json file. Ensure the firebase-tools.json file is formatted correctly"
67
UPLOAD_RELEASE_NOTES_ERROR = "App Distribution halted because it had a problem uploading release notes"
78
UPLOAD_TESTERS_ERROR = "App Distribution halted because it had a problem adding testers/groups"
89
GET_RELEASE_TIMEOUT = "App Distribution failed to fetch release information"
@@ -20,6 +21,7 @@ module ErrorMessage
2021
PLAY_IAS_TERMS_NOT_ACCEPTED = "You must accept the Play Internal App Sharing (IAS) terms to upload AABs."
2122
INVALID_EMAIL_ADDRESS = "You passed an invalid email address."
2223
TESTER_LIMIT_VIOLATION = "Creating testers would exceed tester limit"
24+
EMPTY_TOKENS_FIELD = "Unable to find \"tokens\" field in the firebase-tools.json file. Ensure that the file has a tokens field and try again"
2325

2426
def self.aab_upload_error(aab_state)
2527
"Failed to process the AAB: #{aab_state}"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Fastlane
22
module FirebaseAppDistribution
3-
VERSION = "0.3.6"
3+
VERSION = "0.3.7"
44
end
55
end

spec/firebase_app_distribution_auth_client_spec.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
let(:fake_firebase_tools_contents) { "{\"tokens\": {\"refresh_token\": \"refresh_token\"} }" }
88
let(:fake_firebase_tools_contents_no_tokens_field) { "{}" }
99
let(:fake_firebase_tools_contents_no_refresh_field) { "{\"tokens\": \"empty\"}" }
10+
let(:fake_firebase_tools_contents_invalid_json) { "\"tokens\": \"empty\"}" }
1011
let(:fake_service_creds) { double("service_account_creds") }
1112
let(:fake_oauth_client) { double("oauth_client") }
1213
let(:payload) { { "access_token" => "service_fake_auth_token" } }
@@ -138,7 +139,7 @@
138139
.and_return(fake_firebase_tools_contents_no_tokens_field)
139140
expect(File).to receive(:exist?).and_return(true)
140141
expect { auth_client.fetch_auth_token(empty_val, empty_val) }
141-
.to raise_error(ErrorMessage::MISSING_CREDENTIALS)
142+
.to raise_error(ErrorMessage::EMPTY_TOKENS_FIELD)
142143
end
143144

144145
it 'crashes if the firebase tools json has no refresh_token field' do
@@ -148,6 +149,14 @@
148149
expect { auth_client.fetch_auth_token(empty_val, empty_val) }
149150
.to raise_error(ErrorMessage::MISSING_CREDENTIALS)
150151
end
152+
153+
it 'crashes if the firebase tools json fails to parse' do
154+
allow(File).to receive(:read)
155+
.and_return(fake_firebase_tools_contents_invalid_json)
156+
expect(File).to receive(:exist?).and_return(true)
157+
expect { auth_client.fetch_auth_token(empty_val, empty_val) }
158+
.to raise_error(ErrorMessage::PARSE_FIREBASE_TOOLS_JSON_ERROR)
159+
end
151160
end
152161
end
153162
end

0 commit comments

Comments
 (0)