Adds a special exception and status message for a handle error (#127)

This commit is contained in:
Thad House
2016-07-03 17:27:06 -07:00
committed by Peter Johnson
parent 36ac37db8c
commit fb865d3ee7
16 changed files with 159 additions and 113 deletions

View File

@@ -42,6 +42,7 @@ static jclass runtimeExCls = nullptr;
static jclass illegalArgExCls = nullptr;
static jclass boundaryExCls = nullptr;
static jclass allocationExCls = nullptr;
static jclass halHandleExCls = nullptr;
static jclass canInvalidBufferExCls = nullptr;
static jclass canMessageNotFoundExCls = nullptr;
static jclass canMessageNotAllowedExCls = nullptr;
@@ -126,12 +127,23 @@ void ThrowAllocationException(JNIEnv *env, int32_t status) {
delete[] buf;
}
void ThrowHalHandleException(JNIEnv *env, int32_t status) {
const char *message = getHALErrorMessage(status);
char *buf = new char[strlen(message) + 30];
sprintf(buf, " Code: $%d. %s", status, message);
env->ThrowNew(halHandleExCls, buf);
delete[] buf;
}
void ReportError(JNIEnv *env, int32_t status, bool do_throw) {
if (status == 0) return;
if (status == NO_AVAILABLE_RESOURCES ||
status == RESOURCE_IS_ALLOCATED) {
ThrowAllocationException(env, status);
}
if (status == HAL_HANDLE_ERROR) {
ThrowHalHandleException(env, status);
}
const char *message = getHALErrorMessage(status);
if (do_throw && status < 0) {
char *buf = new char[strlen(message) + 30];
@@ -277,6 +289,12 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
allocationExCls = static_cast<jclass>(env->NewGlobalRef(local));
if (!allocationExCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("edu/wpi/first/wpilibj/util/HalHandleException");
if (!local) return JNI_ERR;
halHandleExCls = static_cast<jclass>(env->NewGlobalRef(local));
if (!halHandleExCls) return JNI_ERR;
env->DeleteLocalRef(local);
local = env->FindClass("edu/wpi/first/wpilibj/can/CANInvalidBufferException");
if (!local) return JNI_ERR;