Skip to content

Commit 7570061

Browse files
committed
Exception handling
1 parent 757caae commit 7570061

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/EnvVarIDTokenSource.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public EnvVarIDTokenSource(String envVarName, Environment env) {
3030
* @throws IllegalArgumentException if the environment variable name is null or empty, or the
3131
* environment is null.
3232
* @throws DatabricksException if the environment variable is not set or is empty.
33+
* @throws ClassCastException if the environment variable is not valid type.
3334
*/
3435
@Override
3536
public IDToken getIDToken(String audience) {
@@ -47,9 +48,9 @@ public IDToken getIDToken(String audience) {
4748
} catch (IllegalArgumentException e) {
4849
throw new DatabricksException(
4950
"Received empty ID token from environment variable " + envVarName);
50-
} catch (RuntimeException e) {
51+
} catch (ClassCastException e) {
5152
throw new DatabricksException(
52-
"Failed to read environment variable " + envVarName + ": " + e.getMessage(), e);
53+
"Environment variable " + envVarName + " has invalid type: " + e.getMessage(), e);
5354
}
5455
}
5556
}

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/FileIDTokenSource.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ public FileIDTokenSource(String filePath) {
3333

3434
/**
3535
* Retrieves an ID Token from the file. The file is read using UTF-8 encoding and the first line
36-
* is used as the token value.
36+
* is used as the token value. Any leading or trailing whitespace in the token is trimmed.
3737
*
38-
* @param audience The intended recipient of the ID Token (not used).
38+
* @param audience The intended recipient of the ID Token. This parameter is not used in this
39+
* implementation as the token is read directly from the file.
3940
* @return An {@link IDToken} containing the token value from the file.
4041
* @throws IllegalArgumentException if the file path is null or empty.
41-
* @throws DatabricksException if the file path is invalid, the file does not exist, is empty, or
42-
* contains only whitespace.
42+
* @throws DatabricksException in the following cases:
43+
* <ul>
44+
* <li>If the file path is invalid or malformed
45+
* <li>If the file does not exist
46+
* <li>If there are security permission issues accessing the file
47+
* <li>If the file is empty or contains only whitespace
48+
* <li>If the file cannot be read due to I/O errors
49+
* <li>If the token format in the file is invalid
50+
* </ul>
4351
*/
4452
@Override
4553
public IDToken getIDToken(String audience) {
@@ -54,8 +62,17 @@ public IDToken getIDToken(String audience) {
5462
throw new DatabricksException("Invalid file path: " + filePath, e);
5563
}
5664

57-
if (!Files.exists(path)) {
58-
throw new DatabricksException("File " + filePath + " does not exist");
65+
try {
66+
if (!Files.exists(path)) {
67+
throw new DatabricksException("File " + filePath + " does not exist");
68+
}
69+
} catch (SecurityException e) {
70+
throw new DatabricksException(
71+
"Security permission denied when checking if file "
72+
+ filePath
73+
+ " exists: "
74+
+ e.getMessage(),
75+
e);
5976
}
6077

6178
List<String> lines;
@@ -64,6 +81,9 @@ public IDToken getIDToken(String audience) {
6481
} catch (IOException e) {
6582
throw new DatabricksException(
6683
"Failed to read ID token from file " + filePath + ": " + e.getMessage(), e);
84+
} catch (SecurityException e) {
85+
throw new DatabricksException(
86+
"Security permission denied when reading file " + filePath + ": " + e.getMessage(), e);
6787
}
6888

6989
if (lines.isEmpty()) {
@@ -74,7 +94,7 @@ public IDToken getIDToken(String audience) {
7494
String token;
7595
try {
7696
token = lines.get(0).trim();
77-
} catch (IndexOutOfBoundsException | NullPointerException e) {
97+
} catch (IndexOutOfBoundsException e) {
7898
throw new DatabricksException("Invalid token format in file " + filePath);
7999
}
80100
return new IDToken(token);

databricks-sdk-java/src/main/java/com/databricks/sdk/core/oauth/IDToken.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class IDToken {
1212
* Constructs an IDToken with a value.
1313
*
1414
* @param value The ID Token string.
15+
* @throws IllegalArgumentException if the token value is null or empty.
1516
*/
1617
public IDToken(String value) {
1718
if (value == null || value.isEmpty()) {

0 commit comments

Comments
 (0)