Skip to content

Commit

Permalink
Consistently call IOException.getMessage when reporting IO related er…
Browse files Browse the repository at this point in the history
…rors.

Also make sure Zip exceptions are handled explicitly for better error message.

PiperOrigin-RevId: 733472483
  • Loading branch information
gkdn authored and copybara-github committed Mar 4, 2025
1 parent cc038b8 commit 0c8bae6
Show file tree
Hide file tree
Showing 15 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private static void preprocessFiles(
String fileContent = MoreFiles.asCharSource(Paths.get(fileInfo.sourcePath()), UTF_8).read();
processedFileContent = strip(fileContent, annotationNames);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
return;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/java/com/google/j2cl/tools/rta/BazelJ2clRta.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ private static void writeToFile(String filePath, List<String> lines, Problems pr
try {
outputSink.writeLines(lines);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

private static void writeToFile(String filePath, CodeRemovalInfo results, Problems problems) {
try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
results.writeTo(outputStream);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

Expand Down
8 changes: 4 additions & 4 deletions transpiler/java/com/google/j2cl/common/OutputUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void writeToFile(Path outputPath, byte[] content) {
Files.write(outputPath, content);
onFileCreation(outputPath);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

Expand All @@ -84,7 +84,7 @@ private void writeToFile(Path outputPath, ImmutableList<String> chunks) {
}
onFileCreation(outputPath);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

Expand All @@ -101,7 +101,7 @@ private void copyToFile(Path from, Path to) {
from, to, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
onFileCreation(to);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_COPY_FILE, e.toString());
problems.fatal(FatalError.CANNOT_COPY_FILE, e.getMessage());
}
}

Expand Down Expand Up @@ -202,7 +202,7 @@ public static void writeToFile(Path outputPath, byte[] content, Problems problem
Files.createDirectories(outputPath.getParent());
Files.write(outputPath, content);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion transpiler/java/com/google/j2cl/common/Problems.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum FatalError {
FILE_NOT_FOUND("File '%s' not found.", 1),
UNKNOWN_INPUT_TYPE("Cannot recognize input type for file '%s'.", 1),
OUTPUT_LOCATION("Output location '%s' must be a directory or .zip file.", 1),
CANNOT_EXTRACT_ZIP("Cannot extract zip '%s'.", 1),
CANNOT_EXTRACT_ZIP("Cannot extract zip '%s': %s.", 2),
CANNOT_CREATE_ZIP("Cannot create zip '%s': %s.", 2),
CANNOT_CLOSE_ZIP("Cannot close zip: %s.", 1),
CANNOT_CREATE_TEMP_DIR("Cannot create temporary directory: %s.", 1),
Expand Down
6 changes: 5 additions & 1 deletion transpiler/java/com/google/j2cl/common/SourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.zip.ZipException;
import javax.annotation.Nullable;

/** Utilities for tools to process source files. */
Expand Down Expand Up @@ -103,8 +104,11 @@ private static ImmutableList<FileInfo> extractZip(
String zipPath, Path sourcesDir, Problems problems) {
try {
return ZipFiles.unzipFile(new File(zipPath), sourcesDir.toFile(), problems);
} catch (ZipException e) {
problems.fatal(FatalError.CANNOT_EXTRACT_ZIP, zipPath, e.getMessage());
return null;
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_EXTRACT_ZIP, zipPath);
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ private static void writeToFile(String filePath, List<String> contents, Problems
try {
Files.asCharSink(new File(filePath), UTF_8).writeLines(contents);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private List<String> getBinaryNamesOfClassesWithExports(
try {
classPathUrls.add(new File(classPathEntry).toURI().toURL());
} catch (MalformedURLException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static NativeJavaScriptFileResolver create(List<FileInfo> files, Problems
byFullyQualifiedName.put(nativeFile.getFullyQualifiedName(), nativeFile);
}
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private String renderSourceMap(
return SourceMapGeneratorStage.generateSourceMaps(
type, javaSourcePositionByOutputSourcePosition);
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private static ImmutableMap<String, List<String>> buildSourceLinesByFileName(
new File(j2clUnitFilePath).getName(),
java.nio.file.Files.readAllLines(Paths.get(j2clUnitFilePath)));
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
}
return contentsByFileNameBuilder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ public String toJson(Problems problems) {
try {
return JsonFormat.printer().print(build());
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public String toJson(Problems problems) {
try {
return JsonFormat.printer().print(build());
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.toString());
problems.fatal(FatalError.CANNOT_WRITE_FILE, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.annotation.Nullable;
import jsinterop.annotations.JsPackage;
Expand Down Expand Up @@ -142,8 +143,10 @@ private void indexPackageInfo(List<String> classPathEntries) {
}
}
problems.abortIfCancelled();
} catch (ZipException e) {
problems.fatal(FatalError.CANNOT_EXTRACT_ZIP, classPathEntry, e.getMessage());
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
}
}
}
Expand Down Expand Up @@ -189,7 +192,7 @@ public void visit(String name, Object value) {
.setNullMarked(getAnnotation(annotations, NullMarked.class) != null)
.build());
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Library parseFiles(FrontendOptions options) {
}
return Library.newBuilder().setCompilationUnits(compilationUnits.build()).build();
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private static String readSource(FileInfo file, Problems problems) {
try {
return Files.readString(Path.of(file.sourcePath()));
} catch (IOException e) {
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.toString());
problems.fatal(FatalError.CANNOT_OPEN_FILE, e.getMessage());
return null;
}
}
Expand Down

0 comments on commit 0c8bae6

Please sign in to comment.