Include errno name in error messages, if the errno is known.

This commit is contained in:
Adam Murdoch
2013-02-16 11:14:41 +11:00
parent 0c5d0dfe80
commit 01c4f26e97
7 changed files with 26 additions and 13 deletions

View File

@@ -21,14 +21,15 @@
#include "generic.h" #include "generic.h"
void mark_failed_with_message(JNIEnv *env, const char* message, jobject result) { 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); 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); 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 JNIEXPORT jint JNICALL

View File

@@ -29,7 +29,14 @@
#include <string.h> #include <string.h>
void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { 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) { char* java_to_char(JNIEnv *env, jstring string, jobject result) {

View File

@@ -25,7 +25,7 @@
* Marks the given result as failed, using the current value of GetLastError() * Marks the given result as failed, using the current value of GetLastError()
*/ */
void mark_failed_with_errno(JNIEnv *env, const char* message, jobject result) { 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) { jstring wchar_to_java(JNIEnv* env, const wchar_t* chars, size_t len, jobject result) {

View File

@@ -23,7 +23,7 @@
extern "C" { extern "C" {
#endif #endif
#define NATIVE_VERSION 13 #define NATIVE_VERSION 14
/* /*
* Marks the given result as failed, using the given error message * 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 * 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. * Converts the given Java string to a NULL terminated wchar_str. Should call free() when finished.

View File

@@ -19,10 +19,12 @@ package net.rubygrapefruit.platform.internal;
public class FunctionResult { public class FunctionResult {
String message; String message;
int errno; int errno;
private String errorCodeDescription;
void failed(String message, int errno) { void failed(String message, int errno, String errorCodeDescription) {
this.message = message; this.message = message;
this.errno = errno; this.errno = errno;
this.errorCodeDescription = errorCodeDescription;
} }
void failed(String message) { void failed(String message) {
@@ -34,6 +36,9 @@ public class FunctionResult {
} }
public String getMessage() { public String getMessage() {
if (errorCodeDescription != null) {
return String.format("%s (%s errno %d)", message, errorCodeDescription, errno);
}
if (errno != 0) { if (errno != 0) {
return String.format("%s (errno %d)", message, errno); return String.format("%s (errno %d)", message, errno);
} }

View File

@@ -20,7 +20,7 @@ import net.rubygrapefruit.platform.internal.FunctionResult;
import net.rubygrapefruit.platform.internal.MutableSystemInfo; import net.rubygrapefruit.platform.internal.MutableSystemInfo;
public class NativeLibraryFunctions { public class NativeLibraryFunctions {
public static final int VERSION = 13; public static final int VERSION = 14;
public static native int getVersion(); public static native int getVersion();

View File

@@ -53,7 +53,7 @@ class PosixFileTest extends Specification {
then: then:
NativeException e = thrown() 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"() { def "cannot get mode on file that does not exist"() {
@@ -64,7 +64,7 @@ class PosixFileTest extends Specification {
then: then:
NativeException e = thrown() 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"() { def "can create symbolic link"() {
@@ -99,7 +99,7 @@ class PosixFileTest extends Specification {
then: then:
NativeException e = thrown() 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"() { def "cannot read a symlink that is not a symlink"() {