Skip to content

Commit 55a8672

Browse files
committed
more explicit error messages in MojoExecutionException-s
1 parent e3d7cd7 commit 55a8672

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

graalpython/graalpy-maven-plugin/src/main/java/org/graalvm/python/maven/plugin/ManageResourcesMojo.java

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,19 @@ private void manageHome() throws MojoExecutionException {
111111
List<String> pythonHomeExcludes = toSortedArrayList(pythonHome.excludes);
112112

113113
if (Files.isReadable(tag)) {
114+
List<String> lines = null;
114115
try {
115-
var lines = Files.readAllLines(tag);
116-
if (lines.isEmpty() || !graalPyVersion.equals(lines.get(0))) {
117-
getLog().info(String.format("Stale GraalPy home, updating to %s", graalPyVersion));
118-
delete(homeDirectory);
119-
}
120-
if (pythonHomeChanged(pythonHomeIncludes, pythonHomeExcludes, lines)) {
121-
getLog().info(String.format("Deleting GraalPy home due to chenges includes or excludes"));
122-
delete(homeDirectory);
123-
}
116+
lines = Files.readAllLines(tag);
124117
} catch (IOException e) {
125-
throw new MojoExecutionException(e);
118+
throw new MojoExecutionException(String.format("failed to read tag file %s", tag), e);
119+
}
120+
if (lines.isEmpty() || !graalPyVersion.equals(lines.get(0))) {
121+
getLog().info(String.format("Stale GraalPy home, updating to %s", graalPyVersion));
122+
delete(homeDirectory);
123+
}
124+
if (pythonHomeChanged(pythonHomeIncludes, pythonHomeExcludes, lines)) {
125+
getLog().info(String.format("Deleting GraalPy home due to chenges includes or excludes"));
126+
delete(homeDirectory);
126127
}
127128
} else {
128129
getLog().info(String.format("Creating GraalPy %s home", graalPyVersion));
@@ -131,7 +132,7 @@ private void manageHome() throws MojoExecutionException {
131132
try {
132133
Files.createDirectories(homeDirectory.getParent());
133134
} catch (IOException e) {
134-
throw new MojoExecutionException(e);
135+
throw new MojoExecutionException(String.format("failed to create home directory %s", homeDirectory), e);
135136
}
136137
copy(homeDirectory.toAbsolutePath().toString(), pythonHomeIncludes, pythonHomeExcludes);
137138
}
@@ -140,7 +141,7 @@ private void manageHome() throws MojoExecutionException {
140141
write(tag, pythonHomeIncludes, INCLUDE_PREFIX);
141142
write(tag, pythonHomeExcludes, EXCLUDE_PREFIX);
142143
} catch (IOException e) {
143-
throw new MojoExecutionException(e);
144+
throw new MojoExecutionException(String.format("failed to write tag file %s", tag), e);
144145
}
145146
}
146147

@@ -311,7 +312,7 @@ private void delete(Path homeDirectory) throws MojoExecutionException {
311312
.forEach(File::delete);
312313
}
313314
} catch (IOException e) {
314-
new MojoExecutionException(e);
315+
new MojoExecutionException(String.format("failed to delete %s", homeDirectory), e);
315316
}
316317
}
317318

@@ -376,18 +377,19 @@ private void manageVenv() throws MojoExecutionException {
376377
var graalPyVersion = ExecGraalPyMojo.getGraalPyVersion(project);
377378

378379
if (Files.isReadable(tag)) {
380+
List<String> lines = null;
379381
try {
380-
var lines = Files.readAllLines(tag);
381-
if (lines.isEmpty() || !graalPyVersion.equals(lines.get(0))) {
382-
getLog().info(String.format("Stale GraalPy venv, updating to %s", graalPyVersion));
383-
delete(venvDirectory);
384-
} else {
385-
for (int i = 1; i < lines.size(); i++) {
386-
installedPackages.add(lines.get(i));
387-
}
388-
}
382+
lines = Files.readAllLines(tag);
389383
} catch (IOException e) {
390-
throw new MojoExecutionException(e);
384+
throw new MojoExecutionException(String.format("failed to read tag file %s", tag), e);
385+
}
386+
if (lines.isEmpty() || !graalPyVersion.equals(lines.get(0))) {
387+
getLog().info(String.format("Stale GraalPy venv, updating to %s", graalPyVersion));
388+
delete(venvDirectory);
389+
} else {
390+
for (int i = 1; i < lines.size(); i++) {
391+
installedPackages.add(lines.get(i));
392+
}
391393
}
392394
} else {
393395
getLog().info(String.format("Creating GraalPy %s venv", graalPyVersion));
@@ -405,7 +407,7 @@ private void manageVenv() throws MojoExecutionException {
405407
Files.write(tag, List.of(graalPyVersion), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
406408
Files.write(tag, packages, StandardOpenOption.APPEND);
407409
} catch (IOException e) {
408-
throw new MojoExecutionException(e);
410+
throw new MojoExecutionException(String.format("failed to write tag file %s", tag), e);
409411
}
410412
}
411413

@@ -423,7 +425,7 @@ private void runLauncher(String... args) throws MojoExecutionException {
423425
try {
424426
pb.start().waitFor();
425427
} catch (IOException | InterruptedException e) {
426-
throw new MojoExecutionException(e);
428+
throw new MojoExecutionException(String.format("failed to execute launcher command %s", cmd), e);
427429
}
428430
}
429431

@@ -445,7 +447,7 @@ private void runVenvBin(Path venvDirectory, String bin, Collection<String> args)
445447
try {
446448
pb.start().waitFor();
447449
} catch (IOException | InterruptedException e) {
448-
throw new MojoExecutionException(e);
450+
throw new MojoExecutionException(String.format("failed to execute venv command %s", cmd), e);
449451
}
450452
}
451453

@@ -486,7 +488,7 @@ private void generateLaunchers() throws MojoExecutionException {
486488
perms.addAll(List.of(PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_EXECUTE));
487489
Files.setPosixFilePermissions(launcher, perms);
488490
} catch (IOException e) {
489-
throw new MojoExecutionException(e);
491+
throw new MojoExecutionException(String.format("failed to create launcher %s", launcher), e);
490492
}
491493
} else {
492494
// on windows, generate a venv launcher that executes our mvn target
@@ -502,20 +504,20 @@ with open(tl, 'ab') as f:
502504
sz = f.write(cmd.encode('utf-16le'))
503505
f.write(struct.pack("@I", sz)) == 4
504506
""",
505-
launcher,
507+
launcher,
506508
Paths.get(projectPath, "pom.xml").toString(),
507-
projectPath);
509+
projectPath);
508510
File tmp;
509511
try {
510512
tmp = File.createTempFile("create_launcher", ".py");
511513
} catch (IOException e) {
512-
throw new MojoExecutionException(e);
514+
throw new MojoExecutionException("failed to create tmp launcher", e);
513515
}
514516
tmp.deleteOnExit();
515517
try (var wr = new FileWriter(tmp)) {
516518
wr.write(script);
517519
} catch (IOException e) {
518-
throw new MojoExecutionException(e);
520+
throw new MojoExecutionException(String.format("failed to write tmp launcher %s", tmp), e);
519521
}
520522
ExecGraalPyMojo.runGraalPy(project, getLog(), tmp.getAbsolutePath());
521523
}

0 commit comments

Comments
 (0)