Skip to content

Commit

Permalink
Showing 3 changed files with 26 additions and 45 deletions.
41 changes: 1 addition & 40 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -4045,46 +4045,7 @@ public RaiseException newStandardError(String message) {
* TODO: Should we include this into Errno code somewhere do we can use this from other places as well?
*/
public RaiseException newIOErrorFromException(final IOException ex) {
final String errorMessage = ex.getMessage();
if ( errorMessage != null ) {
// All errors to sysread should be SystemCallErrors, but on a closed stream
// Ruby returns an IOError. Java throws same exception for all errors so
// we resort to this hack...
switch ( errorMessage ) {
case "Bad file descriptor" :
if ( ex instanceof ClosedChannelException ) throw newErrnoEBADFError();
break;
case "File not open" :
return newIOError(errorMessage);
case "An established connection was aborted by the software in your host machine" :
return newErrnoECONNABORTEDError();
case "Broken pipe" :
return newErrnoEPIPEError();
case "Connection reset by peer" :
case "An existing connection was forcibly closed by the remote host" :
return newErrnoECONNRESETError();
case "Too many levels of symbolic links" :
return newErrnoELOOPError();
case "Too many open files" :
return newErrnoEMFILEError();
case "Too many open files in system" :
return newErrnoENFILEError();
case "Network is unreachable" :
return newErrnoENETUNREACHError();
case "Address already in use" :
return newErrnoEADDRINUSEError();
default :
if ( Platform.IS_WINDOWS ) {
if ( errorMessage.contains("connection was aborted") ) {
return newErrnoECONNRESETError();
}
}
}
return newRaiseException(getIOError(), errorMessage);
} else if (ex instanceof PortUnreachableException) {
throw newErrnoECONNREFUSEDError();
}
return newRaiseException(getIOError(), "IO Error");
return Helpers.newIOErrorFromException(this, ex);
}

public RaiseException newTypeError(IRubyObject receivedObject, RubyClass expectedType) {
28 changes: 24 additions & 4 deletions core/src/main/java/org/jruby/runtime/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.jruby.runtime;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Array;
import java.lang.reflect.Member;
import org.jruby.platform.Platform;

import java.net.PortUnreachableException;
import java.nio.channels.ClosedChannelException;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
@@ -268,7 +270,8 @@ public static Errno errnoFromException(Throwable t) {
// All errors to sysread should be SystemCallErrors, but on a closed stream
// Ruby returns an IOError. Java throws same exception for all errors so
// we resort to this hack...
switch ( errorMessage ) {

switch (errorMessage) {
case "Bad file descriptor":
return Errno.EBADF;
case "File not open":
@@ -299,10 +302,27 @@ public static Errno errnoFromException(Throwable t) {
case "Is a directory":
return Errno.EISDIR;
}
} else if (t instanceof PortUnreachableException) {
return Errno.ECONNREFUSED;
}
return null;
}

/**
* Java does not give us enough information for specific error conditions
* so we are reduced to divining them through string matches...
*
* TODO: Should ECONNABORTED get thrown earlier in the descriptor itself or is it ok to handle this late?
* TODO: Should we include this into Errno code somewhere do we can use this from other places as well?
*/
public static RaiseException newIOErrorFromException(Ruby runtime, IOException ex) {
Errno errno = errnoFromException(ex);

if (errno == null) throw runtime.newIOError(ex.getLocalizedMessage());

throw runtime.newErrnoFromErrno(errno, ex.getLocalizedMessage());
}

public static RubyModule getNthScopeModule(StaticScope scope, int depth) {
int n = depth;
while (n > 0) {
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/util/io/PosixShim.java
Original file line number Diff line number Diff line change
@@ -436,7 +436,7 @@ public Channel open(String cwd, String path, ModeFlags flags, int perm) {
} catch (ResourceException.TooManySymlinks e) {
errno = Errno.ELOOP;
} catch (IOException e) {
throw new RuntimeException("Unhandled IOException: " + e.getLocalizedMessage(), e);
errno = Helpers.errnoFromException(e);
}
return null;
}

0 comments on commit c5313a0

Please sign in to comment.