From d8cb9ce09f5e591c244f2c3b04ea5bf054865ebb Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Mon, 27 Nov 2017 15:58:03 -0800 Subject: [PATCH] Throw FileNotFoundException when connecting to a missing UDS path 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 --- .../src/main/c/netty_unix_errors.c | 5 +++++ .../src/main/java/io/netty/channel/unix/Errors.java | 8 ++++++++ .../unix/ErrorsStaticallyReferencedJniMethods.java | 1 + 3 files changed, 14 insertions(+) diff --git a/transport-native-unix-common/src/main/c/netty_unix_errors.c b/transport-native-unix-common/src/main/c/netty_unix_errors.c index 5103ceefc1..8298f910b8 100644 --- a/transport-native-unix-common/src/main/c/netty_unix_errors.c +++ b/transport-native-unix-common/src/main/c/netty_unix_errors.c @@ -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; } @@ -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 }, diff --git a/transport-native-unix-common/src/main/java/io/netty/channel/unix/Errors.java b/transport-native-unix-common/src/main/java/io/netty/channel/unix/Errors.java index 581efb96fa..53c5ff2f02 100644 --- a/transport-native-unix-common/src/main/java/io/netty/channel/unix/Errors.java +++ b/transport-native-unix-common/src/main/java/io/netty/channel/unix/Errors.java @@ -17,6 +17,7 @@ package io.netty.channel.unix; import io.netty.util.internal.EmptyArrays; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.ConnectException; import java.net.NoRouteToHostException; @@ -33,6 +34,7 @@ import static io.netty.channel.unix.ErrorsStaticallyReferencedJniMethods.*; */ 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(); @@ -104,6 +106,9 @@ public final class Errors { if (err == ERROR_EISCONN_NEGATIVE) { throw new AlreadyConnectedException(); } + if (err == ERRNO_ENOENT_NEGATIVE) { + throw new FileNotFoundException(); + } throw new ConnectException(method + "(..) failed: " + ERRORS[-err]); } @@ -132,6 +137,9 @@ public final class Errors { 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. diff --git a/transport-native-unix-common/src/main/java/io/netty/channel/unix/ErrorsStaticallyReferencedJniMethods.java b/transport-native-unix-common/src/main/java/io/netty/channel/unix/ErrorsStaticallyReferencedJniMethods.java index 3b4edf7824..82bb696781 100644 --- a/transport-native-unix-common/src/main/java/io/netty/channel/unix/ErrorsStaticallyReferencedJniMethods.java +++ b/transport-native-unix-common/src/main/java/io/netty/channel/unix/ErrorsStaticallyReferencedJniMethods.java @@ -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();