Skip to content

Commit b7ebd96

Browse files
authored
If no auth tokens are found, throw a useful error (#22)
1 parent 4ddc6f3 commit b7ebd96

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

apis/platform_common/Credential.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class PlatformRefreshTokenCredential : PlatformCredential {
3434
private const string DummyUserId = "dummy_user_id";
3535
private const string AuthorizationServerUrl = "https://auth.improbable.io/auth/v1/authcode";
3636
private const string TokenServerUrl = "https://auth.improbable.io/auth/v1/token";
37+
private const string RefreshTokenNotFoundMessage = "Please check if environment variable "+DefaultTokenFileEnvVar+" is set to the correct path or try running `spatial init` to fetch and store a new refresh token locally.";
3738

3839
private static readonly Lazy<PlatformRefreshTokenCredential> AutoDetectedLazy =
3940
new Lazy<PlatformRefreshTokenCredential>(GetTokenCredentialAutomatically);
@@ -101,13 +102,20 @@ private static PlatformRefreshTokenCredential GetTokenCredentialAutomatically()
101102
Path.Combine(Environment.GetEnvironmentVariable("HOME") ?? "", ".improbable/oauth2/oauth2_refresh_token"),
102103
Path.Combine(Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%"), ".improbable/oauth2/oauth2_refresh_token")
103104
};
105+
106+
var tokenFile = possibleTokenFiles.FirstOrDefault(File.Exists);
107+
if (tokenFile == null)
108+
{
109+
// None of the possible token files exists
110+
throw new NoRefreshTokenFoundException(RefreshTokenNotFoundMessage);
111+
}
112+
104113
try {
105-
var refreshToken = File.ReadAllText(possibleTokenFiles.FirstOrDefault(File.Exists));
114+
var refreshToken = File.ReadAllText(tokenFile);
106115
return new PlatformRefreshTokenCredential(refreshToken);
107116
}
108117
catch (IOException) {
109-
throw new NoRefreshTokenFoundException(
110-
$"Please check if environment variable {DefaultTokenFileEnvVar} is set to the correct path or try running `spatial init` to fetch and store a new refresh token locally.");
118+
throw new NoRefreshTokenFoundException(RefreshTokenNotFoundMessage);
111119
}
112120
}
113121
}

0 commit comments

Comments
 (0)