Skip to content

Commit 7df1c7a

Browse files
committed
Better exception message generation.
1 parent 47a64bf commit 7df1c7a

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.perl6.nqp.runtime;
2+
3+
import java.io.FileNotFoundException;
4+
import java.nio.file.AccessDeniedException;
5+
import java.nio.file.DirectoryNotEmptyException;
6+
import java.nio.file.FileAlreadyExistsException;
7+
import java.nio.file.NoSuchFileException;
8+
import java.nio.file.NotDirectoryException;
9+
import java.nio.file.NotLinkException;
10+
11+
public class IOExceptionMessages {
12+
13+
public static String message(Exception e) {
14+
if (e instanceof FileNotFoundException
15+
|| e instanceof NoSuchFileException) {
16+
return "File " + e.getMessage() + " not found";
17+
} else if (e instanceof AccessDeniedException) {
18+
return "Access to " + e.getMessage() + " is denied";
19+
} else if (e instanceof DirectoryNotEmptyException) {
20+
return "Directory " + e.getMessage() + " not empty";
21+
} else if (e instanceof FileAlreadyExistsException) {
22+
return "File " + e.getMessage() + " already exists";
23+
} else if (e instanceof NotDirectoryException) {
24+
return "File " + e.getMessage() + " is not a directory";
25+
} else if (e instanceof NotLinkException) {
26+
return "File " + e.getMessage() + " is not a link";
27+
} else {
28+
return e.getClass().getSimpleName() + ": " + e.getMessage();
29+
}
30+
}
31+
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,8 @@ public static long chmod(String path, long mode, ThreadContext tc) {
611611
Set<PosixFilePermission> perms = modeToPosixFilePermission(mode);
612612
Files.setPosixFilePermissions(path_o, perms);
613613
}
614-
catch (IOException e) {
615-
die_s("nqp::chmod: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
616-
}
617614
catch (Exception e) {
618-
die_s("nqp::chmod: unhandled exception", tc);
615+
die_s(IOExceptionMessages.message(e), tc);
619616
}
620617
return 0;
621618
}
@@ -626,11 +623,8 @@ public static long unlink(String path, ThreadContext tc) {
626623
return -2;
627624
}
628625
}
629-
catch (IOException e) {
630-
die_s("nqp::unlink: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
631-
}
632626
catch (Exception e) {
633-
die_s("nqp::unlink: unhandled exception", tc);
627+
die_s(IOExceptionMessages.message(e), tc);
634628
}
635629
return 0;
636630
}
@@ -643,11 +637,8 @@ public static long rmdir(String path, ThreadContext tc) {
643637
}
644638
Files.delete(path_o);
645639
}
646-
catch (IOException e) {
647-
die_s("nqp::rmdir: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
648-
}
649640
catch (Exception e) {
650-
die_s("nqp::rmdir: unhandled exception", tc);
641+
die_s(IOExceptionMessages.message(e), tc);
651642
}
652643
return 0;
653644
}
@@ -666,13 +657,9 @@ public static long mkdir(String path, long mode, ThreadContext tc) {
666657
Files.createDirectory(Paths.get(path),
667658
PosixFilePermissions.asFileAttribute(modeToPosixFilePermission(mode)));
668659
}
669-
catch (IOException e) {
670-
die_s("nqp::mkdir: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
671-
}
672660
catch (Exception e) {
673-
die_s("nqp::mkdir: unhandled exception", tc);
661+
die_s(IOExceptionMessages.message(e), tc);
674662
}
675-
676663
return 0;
677664
}
678665

@@ -682,11 +669,8 @@ public static long rename(String before, String after, ThreadContext tc) {
682669
try {
683670
Files.move(before_o, after_o);
684671
}
685-
catch (IOException e) {
686-
die_s("nqp::rename: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
687-
}
688672
catch (Exception e) {
689-
die_s("nqp::rename: unhandled exception", tc);
673+
die_s(IOExceptionMessages.message(e), tc);
690674
}
691675
return 0;
692676
}
@@ -697,11 +681,8 @@ public static long copy(String before, String after, ThreadContext tc) {
697681
try {
698682
Files.copy(before_o, after_o);
699683
}
700-
catch (IOException e) {
701-
die_s("nqp::copy: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
702-
}
703684
catch (Exception e) {
704-
die_s("nqp::copy: unhandled exception", tc);
685+
die_s(IOExceptionMessages.message(e), tc);
705686
}
706687
return 0;
707688
}
@@ -712,11 +693,8 @@ public static long link(String before, String after, ThreadContext tc) {
712693
try {
713694
Files.createLink(before_o, after_o);
714695
}
715-
catch (IOException e) {
716-
die_s("nqp::link: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
717-
}
718696
catch (Exception e) {
719-
die_s("nqp::link: unhandled exception", tc);
697+
die_s(IOExceptionMessages.message(e), tc);
720698
}
721699
return 0;
722700
}
@@ -768,11 +746,8 @@ public static long symlink(String before, String after, ThreadContext tc) {
768746
try {
769747
Files.createSymbolicLink(before_o, after_o);
770748
}
771-
catch (IOException e) {
772-
die_s("nqp::symlink: " + e.getClass().getSimpleName() + ": " + e.getMessage(), tc);
773-
}
774749
catch (Exception e) {
775-
die_s("nqp::symlink: unhandled exception", tc);
750+
die_s(IOExceptionMessages.message(e), tc);
776751
}
777752
return 0;
778753
}

0 commit comments

Comments
 (0)