From a518a9a3e29fdc69a3be9b0b1aac2e612024eb98 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Wed, 18 Oct 2023 10:08:48 -0500 Subject: [PATCH] Improve IO error translation 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. --- .../main/java/org/jruby/runtime/Helpers.java | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/jruby/runtime/Helpers.java b/core/src/main/java/org/jruby/runtime/Helpers.java index 935a505d200..221f01da983 100644 --- a/core/src/main/java/org/jruby/runtime/Helpers.java +++ b/core/src/main/java/org/jruby/runtime/Helpers.java @@ -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; @@ -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; @@ -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; @@ -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 } @@ -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; @@ -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; }