From 01c4f26e970465a3ac7e5f15edc7ed2c05d89a9a Mon Sep 17 00:00:00 2001 From: Adam Murdoch Date: Sat, 16 Feb 2013 11:14:41 +1100 Subject: [PATCH] Include errno name in error messages, if the errno is known. --- src/main/cpp/generic.cpp | 9 +++++---- src/main/cpp/generic_posix.cpp | 9 ++++++++- src/main/cpp/win.cpp | 2 +- src/main/headers/generic.h | 4 ++-- .../rubygrapefruit/platform/internal/FunctionResult.java | 7 ++++++- .../platform/internal/jni/NativeLibraryFunctions.java | 2 +- .../net/rubygrapefruit/platform/PosixFileTest.groovy | 6 +++--- 7 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/cpp/generic.cpp b/src/main/cpp/generic.cpp index 61830c0..e6813c6 100755 --- a/src/main/cpp/generic.cpp +++ b/src/main/cpp/generic.cpp @@ -21,14 +21,15 @@ #include "generic.h" void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) { - mark_failed_with_code(env, message, 0, result); + mark_failed_with_code(env, message, 0, NULL, result); } -void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result) { +void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, const char* error_code_message, jobject result) { jclass destClass = env->GetObjectClass(result); - jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;I)V"); + jmethodID method = env->GetMethodID(destClass, "failed", "(Ljava/lang/String;ILjava/lang/String;)V"); jstring message_str = env->NewStringUTF(message); - env->CallVoidMethod(result, method, message_str, error_code); + jstring error_code_str = error_code_message == NULL ? NULL : env->NewStringUTF(error_code_message); + env->CallVoidMethod(result, method, message_str, error_code, error_code_str); } JNIEXPORT jint JNICALL diff --git a/src/main/cpp/generic_posix.cpp b/src/main/cpp/generic_posix.cpp index f1cc4d4..f05c8a9 100644 --- a/src/main/cpp/generic_posix.cpp +++ b/src/main/cpp/generic_posix.cpp @@ -29,7 +29,14 @@ #include void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { - mark_failed_with_code(env, message, errno, result); + const char * errno_message = NULL; + switch(errno) { + case ENOENT: + errno_message = "ENOENT"; + break; + } + + mark_failed_with_code(env, message, errno, errno_message, result); } char* java_to_char(JNIEnv *env, jstring string, jobject result) { diff --git a/src/main/cpp/win.cpp b/src/main/cpp/win.cpp index 1bb4f68..9801b99 100755 --- a/src/main/cpp/win.cpp +++ b/src/main/cpp/win.cpp @@ -25,7 +25,7 @@ * Marks the given result as failed, using the current value of GetLastError() */ void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { - mark_failed_with_code(env, message, GetLastError(), result); + mark_failed_with_code(env, message, GetLastError(), NULL, result); } jstring wchar_to_java(JNIEnv* env, const wchar_t* chars, size_t len, jobject result) { diff --git a/src/main/headers/generic.h b/src/main/headers/generic.h index 6924568..f9a621c 100755 --- a/src/main/headers/generic.h +++ b/src/main/headers/generic.h @@ -23,7 +23,7 @@ extern "C" { #endif -#define NATIVE_VERSION 13 +#define NATIVE_VERSION 14 /* * Marks the given result as failed, using the given error message @@ -38,7 +38,7 @@ extern void mark_failed_with_errno(JNIEnv *env, const char* message, jobject res /* * Marks the given result as failed, using the given error message and error code */ -extern void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, jobject result); +extern void mark_failed_with_code(JNIEnv *env, const char* message, int error_code, const char* error_code_message, jobject result); /* * Converts the given Java string to a NULL terminated wchar_str. Should call free() when finished. diff --git a/src/main/java/net/rubygrapefruit/platform/internal/FunctionResult.java b/src/main/java/net/rubygrapefruit/platform/internal/FunctionResult.java index 5377133..002b40f 100644 --- a/src/main/java/net/rubygrapefruit/platform/internal/FunctionResult.java +++ b/src/main/java/net/rubygrapefruit/platform/internal/FunctionResult.java @@ -19,10 +19,12 @@ package net.rubygrapefruit.platform.internal; public class FunctionResult { String message; int errno; + private String errorCodeDescription; - void failed(String message, int errno) { + void failed(String message, int errno, String errorCodeDescription) { this.message = message; this.errno = errno; + this.errorCodeDescription = errorCodeDescription; } void failed(String message) { @@ -34,6 +36,9 @@ public class FunctionResult { } public String getMessage() { + if (errorCodeDescription != null) { + return String.format("%s (%s errno %d)", message, errorCodeDescription, errno); + } if (errno != 0) { return String.format("%s (errno %d)", message, errno); } diff --git a/src/main/java/net/rubygrapefruit/platform/internal/jni/NativeLibraryFunctions.java b/src/main/java/net/rubygrapefruit/platform/internal/jni/NativeLibraryFunctions.java index ddc8da1..c35ab0e 100755 --- a/src/main/java/net/rubygrapefruit/platform/internal/jni/NativeLibraryFunctions.java +++ b/src/main/java/net/rubygrapefruit/platform/internal/jni/NativeLibraryFunctions.java @@ -20,7 +20,7 @@ import net.rubygrapefruit.platform.internal.FunctionResult; import net.rubygrapefruit.platform.internal.MutableSystemInfo; public class NativeLibraryFunctions { - public static final int VERSION = 13; + public static final int VERSION = 14; public static native int getVersion(); diff --git a/src/test/groovy/net/rubygrapefruit/platform/PosixFileTest.groovy b/src/test/groovy/net/rubygrapefruit/platform/PosixFileTest.groovy index acd138d..8221002 100755 --- a/src/test/groovy/net/rubygrapefruit/platform/PosixFileTest.groovy +++ b/src/test/groovy/net/rubygrapefruit/platform/PosixFileTest.groovy @@ -53,7 +53,7 @@ class PosixFileTest extends Specification { then: NativeException e = thrown() - e.message == "Could not set UNIX mode on $testFile: could not chmod file (errno 2)" + e.message == "Could not set UNIX mode on $testFile: could not chmod file (ENOENT errno 2)" } def "cannot get mode on file that does not exist"() { @@ -64,7 +64,7 @@ class PosixFileTest extends Specification { then: NativeException e = thrown() - e.message == "Could not get UNIX mode on $testFile: could not stat file (errno 2)" + e.message == "Could not get UNIX mode on $testFile: could not stat file (ENOENT errno 2)" } def "can create symbolic link"() { @@ -99,7 +99,7 @@ class PosixFileTest extends Specification { then: NativeException e = thrown() - e.message == "Could not read symlink $symlinkFile: could not lstat file (errno 2)" + e.message == "Could not read symlink $symlinkFile: could not lstat file (ENOENT errno 2)" } def "cannot read a symlink that is not a symlink"() {