Skip to content

Commit 9085a76

Browse files
authored
Prefer Java exceptions (#2362)
1 parent c93991e commit 9085a76

11 files changed

Lines changed: 37 additions & 28 deletions

File tree

schemacrawler-commandline/src/main/java/schemacrawler/tools/commandline/command/PasswordOptions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import java.io.BufferedReader;
1515
import java.io.IOException;
16+
import java.io.UncheckedIOException;
1617
import java.nio.file.Path;
1718
import picocli.CommandLine.Option;
18-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
1919
import us.fatehi.utility.ioresource.FileInputResource;
2020

2121
public final class PasswordOptions {
@@ -95,7 +95,7 @@ private String getPasswordFromFile() {
9595
return reader.readLine();
9696
}
9797
} catch (final IOException e) {
98-
throw new IORuntimeException(
98+
throw new UncheckedIOException(
9999
"Password could not be read from file <%s>".formatted(passwordFile), e);
100100
}
101101
}

schemacrawler-commandline/src/main/java/schemacrawler/tools/commandline/command/UserOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
import java.io.BufferedReader;
1515
import java.io.IOException;
16+
import java.io.UncheckedIOException;
1617
import java.nio.file.Path;
1718
import picocli.CommandLine.Option;
18-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
1919
import us.fatehi.utility.ioresource.FileInputResource;
2020

2121
public final class UserOptions {
@@ -95,7 +95,8 @@ private String getUserFromFile() {
9595
return reader.readLine();
9696
}
9797
} catch (final IOException e) {
98-
throw new IORuntimeException("User could not be read from file <%s>".formatted(userFile), e);
98+
throw new UncheckedIOException(
99+
"User could not be read from file <%s>".formatted(userFile), e);
99100
}
100101
}
101102

schemacrawler-commandline/src/main/java/schemacrawler/tools/commandline/utility/GenerateCliSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package schemacrawler.tools.commandline.utility;
1010

1111
import java.io.IOException;
12+
import java.io.UncheckedIOException;
1213
import java.nio.file.Files;
1314
import java.nio.file.Path;
1415
import java.util.Arrays;
@@ -18,7 +19,6 @@
1819
import picocli.CommandLine;
1920
import picocli.CommandLine.Command;
2021
import picocli.codegen.docgen.manpage.ManPageGenerator;
21-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
2222
import schemacrawler.schemacrawler.exceptions.InternalRuntimeException;
2323
import schemacrawler.tools.commandline.SchemaCrawlerCommandLineCommands;
2424
import schemacrawler.tools.commandline.command.ConfigFileCommand;
@@ -77,7 +77,7 @@ protected Path createOutputDirectory() {
7777
try {
7878
Files.createDirectories(outputDir);
7979
} catch (final IOException e) {
80-
throw new IORuntimeException("Could not create output directory", e);
80+
throw new UncheckedIOException("Could not create output directory", e);
8181
}
8282
return outputDir.toAbsolutePath();
8383
}

schemacrawler-diagram/src/main/java/schemacrawler/tools/command/text/diagram/AbstractGraphProcessExecutor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
import static us.fatehi.utility.IOUtility.isFileReadable;
1313
import static us.fatehi.utility.IOUtility.isFileWritable;
1414

15+
import java.io.IOException;
16+
import java.io.UncheckedIOException;
1517
import java.nio.file.Path;
16-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
1718
import schemacrawler.tools.command.text.diagram.options.DiagramOutputFormat;
1819

1920
abstract class AbstractGraphProcessExecutor implements GraphExecutor {
@@ -33,11 +34,15 @@ protected AbstractGraphProcessExecutor(
3334
this.diagramOutputFormat = diagramOutputFormat;
3435

3536
if (!isFileReadable(this.dotFile)) {
36-
throw new IORuntimeException("Cannot read DOT file <%s>".formatted(this.dotFile));
37+
final IOException cause =
38+
new IOException("Cannot read DOT file <%s>".formatted(this.dotFile));
39+
throw new UncheckedIOException(cause);
3740
}
3841

3942
if (!isFileWritable(this.outputFile)) {
40-
throw new IORuntimeException("Cannot write output file <%s>".formatted(this.outputFile));
43+
final IOException cause =
44+
new IOException("Cannot write output file <%s>".formatted(this.outputFile));
45+
throw new UncheckedIOException(cause);
4146
}
4247
}
4348
}

schemacrawler-diagram/src/main/java/schemacrawler/tools/command/text/diagram/DiagramRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
import static us.fatehi.utility.IOUtility.readResourceFully;
1515

1616
import java.io.IOException;
17+
import java.io.UncheckedIOException;
1718
import java.nio.file.Path;
1819
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
19-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
2020
import schemacrawler.schemacrawler.exceptions.SchemaCrawlerException;
2121
import schemacrawler.tools.command.AbstractSchemaCrawlerCommand;
2222
import schemacrawler.tools.command.text.diagram.options.DiagramOptions;
@@ -60,7 +60,7 @@ public void execute() {
6060
try {
6161
dotFile = createTempFilePath("schemacrawler.", "dot");
6262
} catch (final IOException e) {
63-
throw new IORuntimeException("Could not create temporary DOT file", e);
63+
throw new UncheckedIOException("Could not create temporary DOT file", e);
6464
}
6565
final OutputOptions dotFileOutputOptions;
6666
if (diagramOutputFormat == scdot) {

schemacrawler-diagram/src/main/java/schemacrawler/tools/command/text/embeddeddiagram/EmbeddedDiagramRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import java.io.BufferedReader;
2525
import java.io.BufferedWriter;
2626
import java.io.IOException;
27+
import java.io.UncheckedIOException;
2728
import java.io.Writer;
2829
import java.nio.file.Path;
2930
import java.util.regex.Pattern;
3031
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
31-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
3232
import schemacrawler.schemacrawler.exceptions.SchemaCrawlerException;
3333
import schemacrawler.tools.command.AbstractSchemaCrawlerCommand;
3434
import schemacrawler.tools.command.SchemaCrawlerCommand;
@@ -110,7 +110,7 @@ public void execute() {
110110
copy(newBufferedReader(finalHtmlFile, UTF_8), writer);
111111
}
112112
} catch (final IOException e) {
113-
throw new IORuntimeException("Could not create embedded diagram", e);
113+
throw new UncheckedIOException("Could not create embedded diagram", e);
114114
} catch (final SchemaCrawlerException e) {
115115
throw e;
116116
} catch (final Exception e) {

schemacrawler-diagram/src/test/java/schemacrawler/integration/test/DiagramOutputTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import static java.nio.file.Files.createDirectories;
1212
import static org.apache.commons.io.FileUtils.deleteDirectory;
13-
import static org.hamcrest.CoreMatchers.startsWith;
13+
import static org.hamcrest.CoreMatchers.containsString;
1414
import static org.hamcrest.MatcherAssert.assertThat;
1515
import static org.hamcrest.Matchers.instanceOf;
1616
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -22,6 +22,7 @@
2222
import static us.fatehi.test.utility.extensions.FileHasContent.classpathResource;
2323
import static us.fatehi.test.utility.extensions.FileHasContent.outputOf;
2424

25+
import java.io.UncheckedIOException;
2526
import java.nio.file.Path;
2627
import java.util.List;
2728
import org.junit.jupiter.api.BeforeAll;
@@ -37,7 +38,6 @@
3738
import schemacrawler.schemacrawler.SchemaRetrievalOptions;
3839
import schemacrawler.schemacrawler.SchemaRetrievalOptionsBuilder;
3940
import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException;
40-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
4141
import schemacrawler.test.utility.DatabaseTestUtility;
4242
import schemacrawler.test.utility.WithTestDatabase;
4343
import schemacrawler.tools.command.text.diagram.options.DiagramOptions;
@@ -181,8 +181,8 @@ public void executableForDiagram_badOutputFile(
181181
final ExecutionRuntimeException runtimeException =
182182
assertThrows(ExecutionRuntimeException.class, () -> executable.execute());
183183
final Throwable exception = runtimeException.getCause();
184-
assertThat(exception, instanceOf(IORuntimeException.class));
185-
assertThat(exception.getMessage(), startsWith("Cannot write output file"));
184+
assertThat(exception, instanceOf(UncheckedIOException.class));
185+
assertThat(exception.getMessage(), containsString("Cannot write output file"));
186186
}
187187

188188
@Test

schemacrawler-diagram/src/test/java/schemacrawler/tools/command/text/diagram/GraphProcessExecutorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import static us.fatehi.test.utility.extensions.FileHasContent.outputOf;
2020

2121
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.util.List;
2526
import org.junit.jupiter.api.Test;
26-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
2727
import schemacrawler.tools.command.text.diagram.options.DiagramOutputFormat;
2828
import us.fatehi.test.utility.extensions.CaptureSystemStreams;
2929
import us.fatehi.test.utility.extensions.CapturedSystemStreams;
@@ -63,18 +63,18 @@ public void constructorGraphProcessExecutor() throws IOException {
6363
is(not(nullValue())));
6464

6565
// DOT file not readable
66-
final IORuntimeException exception1 =
66+
final UncheckedIOException exception1 =
6767
assertThrows(
68-
IORuntimeException.class,
68+
UncheckedIOException.class,
6969
() ->
7070
new TestGraphProcessExecutor(
7171
IOUtility.createTempFilePath("sc", "data"), outputFile, diagramOutputFormat));
7272
assertThat(exception1.getMessage(), containsString("Cannot read DOT file"));
7373

7474
// Output file not writable
75-
final IORuntimeException exception2 =
75+
final UncheckedIOException exception2 =
7676
assertThrows(
77-
IORuntimeException.class,
77+
UncheckedIOException.class,
7878
() ->
7979
new TestGraphProcessExecutor(
8080
dotFile, Path.of("/not_a_directory/unwritable_file.dat"), diagramOutputFormat));

schemacrawler-scripting/src/main/java/schemacrawler/tools/command/script/GraalScriptExecutor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.IOException;
1515
import java.io.OutputStream;
1616
import java.io.Reader;
17+
import java.io.UncheckedIOException;
1718
import java.io.Writer;
1819
import java.util.HashMap;
1920
import java.util.Map;
@@ -23,7 +24,6 @@
2324
import org.graalvm.polyglot.Context;
2425
import org.graalvm.polyglot.Source;
2526
import org.graalvm.polyglot.Value;
26-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
2727
import schemacrawler.tools.command.script.options.ScriptLanguageType;
2828

2929
/** Main executor for the Graal Polyglot integration. */
@@ -121,7 +121,7 @@ public void run() {
121121
// Evaluate the script
122122
graalPolyglotContext.eval(sourceCode);
123123
} catch (final IOException e) {
124-
throw new IORuntimeException(e.getMessage(), e);
124+
throw new UncheckedIOException(e.getMessage(), e);
125125
}
126126
}
127127
}

schemacrawler-scripting/src/main/java/schemacrawler/tools/command/serialize/SerializationCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
import java.io.IOException;
1717
import java.io.OutputStream;
18+
import java.io.UncheckedIOException;
1819
import java.io.Writer;
1920
import java.nio.file.Path;
2021
import java.util.zip.GZIPOutputStream;
21-
import schemacrawler.schemacrawler.exceptions.IORuntimeException;
2222
import schemacrawler.tools.command.AbstractSchemaCrawlerCommand;
2323
import schemacrawler.tools.command.serialize.options.SerializationFormat;
2424
import schemacrawler.tools.command.serialize.options.SerializationOptions;
@@ -57,7 +57,7 @@ public void execute() {
5757
new GZIPOutputStream(newOutputStream(outputFile, WRITE, CREATE, TRUNCATE_EXISTING))) {
5858
catalogSerializer.save(out);
5959
} catch (final IOException e) {
60-
throw new IORuntimeException("Could not save catalog", e);
60+
throw new UncheckedIOException("Could not save catalog", e);
6161
}
6262
} else {
6363
final Writer out = outputOptions.openNewOutputWriter();

0 commit comments

Comments
 (0)