Skip to content

Commit

Permalink
Improve IO error translation
Browse files Browse the repository at this point in the history
The case of SocketException("Connection reset") was not handled,
and IOException only checked for one message. This combines the
logic for errnoFromException with the logic for errnorFromMessage.

The exception handling stack was also cleaned up here.
  • Loading branch information
headius committed Oct 31, 2023
1 parent 56eec66 commit a518a9a
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.net.BindException;
import java.net.PortUnreachableException;
import java.net.SocketException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NonReadableChannelException;
import java.nio.channels.NonWritableChannelException;
Expand Down Expand Up @@ -257,7 +258,7 @@ public static Errno errnoFromException(Throwable t) {
// Try specific exception types by rethrowing and catching.
try {
throw t;
} catch (FileNotFoundException fnfe) {
} catch (FileNotFoundException | NoSuchFileException fnfe) {
return Errno.ENOENT;
} catch (EOFException eofe) {
return Errno.EPIPE;
Expand All @@ -271,15 +272,11 @@ public static Errno errnoFromException(Throwable t) {
return Errno.EEXIST;
} catch (FileSystemLoopException fsle) {
return Errno.ELOOP;
} catch (NoSuchFileException nsfe) {
return Errno.ENOENT;
} catch (NotDirectoryException nde) {
return Errno.ENOTDIR;
} catch (AccessDeniedException ade) {
return Errno.EACCES;
} catch (DirectoryNotEmptyException dnee) {
return errnoFromMessage(dnee);
} catch (BindException be) {
} catch (IOException be) {
return errnoFromMessage(be);
} catch (NotYetConnectedException nyce) {
return Errno.ENOTCONN;
Expand All @@ -288,12 +285,6 @@ public static Errno errnoFromException(Throwable t) {
return Errno.EINVAL;
} catch (IllegalArgumentException nrce) {
return Errno.EINVAL;
} catch (IOException ioe) {
String message = ioe.getMessage();
// Raised on Windows for process launch with missing file
if (message.endsWith("The system cannot find the file specified")) {
return Errno.ENOENT;
}
} catch (Throwable t2) {
// fall through
}
Expand All @@ -319,6 +310,7 @@ private static Errno errnoFromMessage(Throwable t) {
return Errno.ECONNABORTED;
case "Broken pipe":
return Errno.EPIPE;
case "Connection reset":
case "Connection reset by peer":
case "An existing connection was forcibly closed by the remote host":
return Errno.ECONNRESET;
Expand Down Expand Up @@ -352,7 +344,13 @@ private static Errno errnoFromMessage(Throwable t) {
case "Protocol family not supported":
return Errno.EPFNOSUPPORT;
}

// Raised on Windows for process launch with missing file
if (errorMessage.endsWith("The system cannot find the file specified")) {
return Errno.ENOENT;
}
}

return null;
}

Expand Down

0 comments on commit a518a9a

Please sign in to comment.