Skip to content

Commit

Permalink
Improve support for IOExceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwpark committed Feb 21, 2025
1 parent 2496e06 commit afa4bfc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
41 changes: 31 additions & 10 deletions src/driver/src/main/java/com/gel/driver/GelConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.");
}
}
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.");
}
}
}

Expand Down Expand Up @@ -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
));
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions src/driver/src/main/java/com/gel/driver/util/ConfigUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ public static DatabaseOrBranch ofBranch(@NotNull String value) {

public static final class ResolvedField<T> {
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;
}
Expand All @@ -273,12 +273,18 @@ private ResolvedField(@Nullable T value, @Nullable ConfigurationException error)
public static @NotNull <T>ResolvedField<T> invalid(@NotNull ConfigurationException error) {
return new ResolvedField<T>(null, error);
}
public static @NotNull <T>ResolvedField<T> invalid(@NotNull IOException error) {
return new ResolvedField<T>(null, error);
}
private static @NotNull <T>ResolvedField<T> invalid(@NotNull Exception error) {
return new ResolvedField<T>(null, error);
}

public final @Nullable T getValue() {
return value;
}

public @Nullable ConfigurationException getError() {
public @Nullable Exception getError() {
return error;
}

Expand All @@ -302,7 +308,7 @@ private ResolvedField(@Nullable T value, @Nullable ConfigurationException error)

public static @Nullable <T> T checkAndGetFieldValue(
@Nullable ResolvedField<T> field
) throws ConfigurationException {
) throws ConfigurationException, IOException {
return checkAndGetFieldValue(field, null);
}

Expand All @@ -314,7 +320,7 @@ public interface Checker<T> {
public static @Nullable <T> T checkAndGetFieldValue(
@Nullable ResolvedField<T> field,
@Nullable Checker<T> checker
) throws ConfigurationException {
) throws ConfigurationException, IOException {
if (field == null) {
return null;
}
Expand All @@ -324,7 +330,15 @@ public interface Checker<T> {
}

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();
Expand Down Expand Up @@ -367,7 +381,7 @@ public static HashMap<String, ResolvedField<String>> addServerSettingField(

public static HashMap<String, String> checkAndGetServerSettings(
HashMap<String, ResolvedField<String>> serverSettings
) throws ConfigurationException {
) throws ConfigurationException, IOException {
HashMap<String, String> result = new HashMap<String, String>();
for (Map.Entry<String, ResolvedField<String>> entry : serverSettings.entrySet())
{
Expand Down
9 changes: 6 additions & 3 deletions src/driver/src/test/java/SharedClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
)
);
Expand All @@ -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")
)
),
Expand Down Expand Up @@ -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")
)
),
Expand Down

0 comments on commit afa4bfc

Please # to comment.