Skip to content

Commit

Permalink
Throw FileNotFoundException when connecting to a missing UDS path
Browse files Browse the repository at this point in the history
Motivation:
Exception handling is nicer when a more specific Exception is thrown

Modification:
Add a static reference for ENOENT, and throw FNFE if it is returned

Result:
More precise exception handling
  • Loading branch information
carl-mastrangelo authored and normanmaurer committed Nov 28, 2017
1 parent 460d125 commit d8cb9ce
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
5 changes: 5 additions & 0 deletions transport-native-unix-common/src/main/c/netty_unix_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void netty_unix_errors_throwOutOfMemoryError(JNIEnv* env) {
}

// JNI Registered Methods Begin
static jint netty_unix_errors_errnoENOENT(JNIEnv* env, jclass clazz) {
return ENOENT;
}

static jint netty_unix_errors_errnoENOTCONN(JNIEnv* env, jclass clazz) {
return ENOTCONN;
}
Expand Down Expand Up @@ -129,6 +133,7 @@ static jstring netty_unix_errors_strError(JNIEnv* env, jclass clazz, jint error)

// JNI Method Registration Table Begin
static const JNINativeMethod statically_referenced_fixed_method_table[] = {
{ "errnoENOENT", "()I", (void *) netty_unix_errors_errnoENOENT },
{ "errnoENOTCONN", "()I", (void *) netty_unix_errors_errnoENOTCONN },
{ "errnoEBADF", "()I", (void *) netty_unix_errors_errnoEBADF },
{ "errnoEPIPE", "()I", (void *) netty_unix_errors_errnoEPIPE },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.util.internal.EmptyArrays;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
Expand All @@ -33,6 +34,7 @@
*/
public final class Errors {
// As all our JNI methods return -errno on error we need to compare with the negative errno codes.
public static final int ERRNO_ENOENT_NEGATIVE = -errnoENOENT();
public static final int ERRNO_ENOTCONN_NEGATIVE = -errnoENOTCONN();
public static final int ERRNO_EBADF_NEGATIVE = -errnoEBADF();
public static final int ERRNO_EPIPE_NEGATIVE = -errnoEPIPE();
Expand Down Expand Up @@ -104,6 +106,9 @@ static void throwConnectException(String method, NativeConnectException refusedC
if (err == ERROR_EISCONN_NEGATIVE) {
throw new AlreadyConnectedException();
}
if (err == ERRNO_ENOENT_NEGATIVE) {
throw new FileNotFoundException();
}
throw new ConnectException(method + "(..) failed: " + ERRORS[-err]);
}

Expand Down Expand Up @@ -132,6 +137,9 @@ public static int ioResult(String method, int err, NativeIoException resetCause,
if (err == ERRNO_ENOTCONN_NEGATIVE) {
throw new NotYetConnectedException();
}
if (err == ERRNO_ENOENT_NEGATIVE) {
throw new FileNotFoundException();
}

// TODO: We could even go further and use a pre-instantiated IOException for the other error codes, but for
// all other errors it may be better to just include a stack trace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class ErrorsStaticallyReferencedJniMethods {

private ErrorsStaticallyReferencedJniMethods() { }

static native int errnoENOENT();
static native int errnoEBADF();
static native int errnoEPIPE();
static native int errnoECONNRESET();
Expand Down

0 comments on commit d8cb9ce

Please sign in to comment.