diff --git a/src/driver/src/main/java/com/gel/driver/GelConnection.java b/src/driver/src/main/java/com/gel/driver/GelConnection.java index 39595a0a..2427ff75 100644 --- a/src/driver/src/main/java/com/gel/driver/GelConnection.java +++ b/src/driver/src/main/java/com/gel/driver/GelConnection.java @@ -710,7 +710,7 @@ public boolean isEmpty() { + "\"CredentialsFile\" or \"Host\"/\"Port\""); // The primaryError has priority, so hold on to any other exception // until all primary options are processed. - @Nullable ConfigurationException deferredPrimaryError = null; + @Nullable Exception deferredPrimaryError = null; if (builder.instance != null) { if (hasPrimaryOptions) { @@ -764,12 +764,18 @@ public boolean isEmpty() { credentialsText = "{}"; } } - else { + else if (builder.credentialsFile.toString().equals("")) { deferredPrimaryError = new ConfigurationException(String.format( "Invalid CredentialsFile: \"%s\", could not find file", builder.credentialsFile )); } + else { + deferredPrimaryError = new FileNotFoundException(String.format( + "Invalid CredentialsFile: \"%s\", could not find file", + builder.credentialsFile + )); + } hasPrimaryOptions = true; } if (credentialsText != null) { @@ -815,7 +821,15 @@ public boolean isEmpty() { } if (deferredPrimaryError != null) { - throw deferredPrimaryError; + if (deferredPrimaryError instanceof ConfigurationException) { + throw (ConfigurationException)deferredPrimaryError; + } + else if (deferredPrimaryError instanceof IOException) { + throw (IOException)deferredPrimaryError; + } + else { + throw new ConfigurationException("Error: Unknown exception."); + } } } @@ -836,8 +850,7 @@ public boolean isEmpty() { + "\"GEL_CREDENTIALS_FILE\" or \"GEL_HOST\"/\"GEL_PORT\""); // The primaryError has priority, so hold on to any other exception // until all primary env vars are processed. - @Nullable ConfigurationException deferredPrimaryError = null; - + @Nullable Exception deferredPrimaryError = null; GelEnvVar instanceEnvVar = SystemProvider.getGelEnvVariable(provider, INSTANCE_ENV_NAME); if (instanceEnvVar != null) { if (hasPrimaryEnv) { @@ -881,7 +894,7 @@ public boolean isEmpty() { } else { - deferredPrimaryError = new ConfigurationException(String.format( + deferredPrimaryError = new FileNotFoundException(String.format( "Invalid credential file from %s: \"%s\", could not find file", credentialsFileEnvVar.name, credentialsFileEnvVar.value @@ -925,7 +938,15 @@ public boolean isEmpty() { } if (deferredPrimaryError != null) { - throw deferredPrimaryError; + if (deferredPrimaryError instanceof ConfigurationException) { + throw (ConfigurationException)deferredPrimaryError; + } + else if (deferredPrimaryError instanceof IOException) { + throw (IOException)deferredPrimaryError; + } + else { + throw new ConfigurationException("Error: Unknown exception."); + } } } @@ -1116,7 +1137,7 @@ else if (builder.branch != null) { ); } else { - throw new ConfigurationException(String.format( + throw new FileNotFoundException(String.format( "Invalid TLSCertificateAuthorityFile: \"%s\", could not find file", builder.tlsCertificateAuthorityFile )); @@ -1161,7 +1182,7 @@ else if (builder.branch != null) { private static GelConnection _fromResolvedFields( @NotNull ConfigUtils.ResolvedFields resolvedFields, @NotNull SystemProvider platform - ) throws ConfigurationException { + ) throws ConfigurationException, IOException { GelConnection result = new GelConnection(); result.hostname = ConfigUtils.checkAndGetFieldValue( @@ -1465,7 +1486,7 @@ else if (key.startsWith("database")) { } else { value = ResolvedField.invalid( - new ConfigurationException(String.format( + new FileNotFoundException(String.format( "Invalid DSN query parameter: \"%s\" could not find file \"%s\"", oldKey, fileName diff --git a/src/driver/src/main/java/com/gel/driver/util/ConfigUtils.java b/src/driver/src/main/java/com/gel/driver/util/ConfigUtils.java index db40b969..ef99ccb9 100644 --- a/src/driver/src/main/java/com/gel/driver/util/ConfigUtils.java +++ b/src/driver/src/main/java/com/gel/driver/util/ConfigUtils.java @@ -260,9 +260,9 @@ public static DatabaseOrBranch ofBranch(@NotNull String value) { public static final class ResolvedField { private final @Nullable T value; - private final @Nullable ConfigurationException error; + private final @Nullable Exception error; - private ResolvedField(@Nullable T value, @Nullable ConfigurationException error) { + private ResolvedField(@Nullable T value, @Nullable Exception error) { this.value = value; this.error = error; } @@ -273,12 +273,18 @@ private ResolvedField(@Nullable T value, @Nullable ConfigurationException error) public static @NotNull ResolvedField invalid(@NotNull ConfigurationException error) { return new ResolvedField(null, error); } + public static @NotNull ResolvedField invalid(@NotNull IOException error) { + return new ResolvedField(null, error); + } + private static @NotNull ResolvedField invalid(@NotNull Exception error) { + return new ResolvedField(null, error); + } public final @Nullable T getValue() { return value; } - public @Nullable ConfigurationException getError() { + public @Nullable Exception getError() { return error; } @@ -302,7 +308,7 @@ private ResolvedField(@Nullable T value, @Nullable ConfigurationException error) public static @Nullable T checkAndGetFieldValue( @Nullable ResolvedField field - ) throws ConfigurationException { + ) throws ConfigurationException, IOException { return checkAndGetFieldValue(field, null); } @@ -314,7 +320,7 @@ public interface Checker { public static @Nullable T checkAndGetFieldValue( @Nullable ResolvedField field, @Nullable Checker checker - ) throws ConfigurationException { + ) throws ConfigurationException, IOException { if (field == null) { return null; } @@ -324,7 +330,15 @@ public interface Checker { } if (field.getError() != null) { - throw field.getError(); + if (field.getError() instanceof ConfigurationException) { + throw (ConfigurationException)field.getError(); + } + else if (field.getError() instanceof IOException) { + throw (IOException)field.getError(); + } + else { + throw new ConfigurationException("Error: Unknown exception."); + } } return field.getValue(); @@ -367,7 +381,7 @@ public static HashMap> addServerSettingField( public static HashMap checkAndGetServerSettings( HashMap> serverSettings - ) throws ConfigurationException { + ) throws ConfigurationException, IOException { HashMap result = new HashMap(); for (Map.Entry> entry : serverSettings.entrySet()) { diff --git a/src/driver/src/test/java/SharedClientTests.java b/src/driver/src/test/java/SharedClientTests.java index 25b8c589..c1fa7d93 100644 --- a/src/driver/src/test/java/SharedClientTests.java +++ b/src/driver/src/test/java/SharedClientTests.java @@ -6,6 +6,7 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; @@ -371,8 +372,10 @@ private static void assertSameException( errorMatch.type, actual.getClass(), String.format( - "%s: %s", + "%s: Wrong exception type \"%s\", expected \"%s\"\n%s", testName, + actual.getClass().getName(), + errorMatch.type.getTypeName(), prettyError(actual) ) ); @@ -399,7 +402,7 @@ public ErrorMatch(@NotNull Type type, @NotNull Pattern text) { entry( "credentials_file_not_found", new ErrorMatch( - ConfigurationException.class, + FileNotFoundException.class, Pattern.compile("cannot read credentials") ) ), @@ -515,7 +518,7 @@ public ErrorMatch(@NotNull Type type, @NotNull Pattern text) { entry( "file_not_found", new ErrorMatch( - ConfigurationException.class, + FileNotFoundException.class, Pattern.compile("could not find file") ) ),