improve native ptql exception messages

This commit is contained in:
Doug MacEachern 2007-04-15 23:26:09 +00:00
parent aa4e73c437
commit 2bb854483f
5 changed files with 29 additions and 19 deletions

View File

@ -114,6 +114,13 @@ static void sigar_throw_notimpl(JNIEnv *env, char *msg)
JENV->ThrowNew(env, errorClass, msg); JENV->ThrowNew(env, errorClass, msg);
} }
static void sigar_throw_ptql_malformed(JNIEnv *env, char *msg)
{
jclass errorClass = SIGAR_FIND_CLASS("ptql/MalformedQueryException");
JENV->ThrowNew(env, errorClass, msg);
}
static void sigar_throw_error(JNIEnv *env, jni_sigar_t *jsigar, int err) static void sigar_throw_error(JNIEnv *env, jni_sigar_t *jsigar, int err)
{ {
jclass errorClass; jclass errorClass;
@ -1199,7 +1206,15 @@ JNIEXPORT void SIGAR_JNI(ptql_SigarProcessQuery_create)
} }
if (status != SIGAR_OK) { if (status != SIGAR_OK) {
sigar_throw_exception(env, "Malformed query"); /*XXX*/ char buf[1024], *msg=buf;
if (status == SIGAR_PTQL_MALFORMED_QUERY) {
msg = "Malformed query";
}
else {
sigar_strerror_get(status, buf, sizeof(buf));
}
sigar_throw_ptql_malformed(env, msg);
} }
else { else {
sigar_set_pointer(env, obj, query); sigar_set_pointer(env, obj, query);

View File

@ -160,11 +160,7 @@ public class ProcessQueryFactory implements Comparator {
if (useNative) { if (useNative) {
pQuery = new SigarProcessQuery(); pQuery = new SigarProcessQuery();
try {
((SigarProcessQuery)pQuery).create(query); ((SigarProcessQuery)pQuery).create(query);
} catch (SigarException e) {
throw new MalformedQueryException(e.getMessage());
}
cache.put(query, pQuery); cache.put(query, pQuery);
return pQuery; return pQuery;
} }

View File

@ -28,7 +28,7 @@ public class SigarProcessQuery implements ProcessQuery {
long longSigarWrapper = 0; //same, but where sizeof(void*) > sizeof(int) long longSigarWrapper = 0; //same, but where sizeof(void*) > sizeof(int)
native void create(String ptql) native void create(String ptql)
throws SigarException; throws MalformedQueryException;
native void destroy(); native void destroy();

View File

@ -19,6 +19,8 @@
#ifndef SIGAR_PTQL_H #ifndef SIGAR_PTQL_H
#define SIGAR_PTQL_H #define SIGAR_PTQL_H
#define SIGAR_PTQL_MALFORMED_QUERY -1
typedef struct sigar_ptql_query_t sigar_ptql_query_t; typedef struct sigar_ptql_query_t sigar_ptql_query_t;
typedef int (*sigar_ptql_re_impl_t)(void *, char *, char *); typedef int (*sigar_ptql_re_impl_t)(void *, char *, char *);

View File

@ -35,9 +35,6 @@
#define strtonum_failed(src, ptr) \ #define strtonum_failed(src, ptr) \
((src == ptr) || (errno == ERANGE) || (*ptr != '\0')) ((src == ptr) || (errno == ERANGE) || (*ptr != '\0'))
/* XXX need more specific errors */
#define SIGAR_PTQL_MALFORMED_QUERY 1
typedef struct ptql_parse_branch_t ptql_parse_branch_t; typedef struct ptql_parse_branch_t ptql_parse_branch_t;
typedef struct ptql_branch_t ptql_branch_t; typedef struct ptql_branch_t ptql_branch_t;
@ -631,7 +628,7 @@ static int ptql_branch_init_pid(ptql_parse_branch_t *parsed,
char *ptr; char *ptr;
branch->data.pid = str2pid(parsed->value, ptr); branch->data.pid = str2pid(parsed->value, ptr);
if (strtonum_failed(parsed->value, ptr)) { if (strtonum_failed(parsed->value, ptr)) {
return SIGAR_PTQL_MALFORMED_QUERY; return EINVAL;
} }
} }
return SIGAR_OK; return SIGAR_OK;
@ -652,7 +649,7 @@ static int ptql_branch_init_pid(ptql_parse_branch_t *parsed,
} }
else { else {
return SIGAR_PTQL_MALFORMED_QUERY; return EINVAL;
} }
} }
@ -675,7 +672,7 @@ static int ptql_pid_match(sigar_t *sigar,
} }
match_pid = strtoull(buffer, &ptr, 10); match_pid = strtoull(buffer, &ptr, 10);
if (strtonum_failed(buffer, ptr)) { if (strtonum_failed(buffer, ptr)) {
return SIGAR_PTQL_MALFORMED_QUERY; return errno;
} }
} }
else if (branch->flags == PTQL_PID_SERVICE) { else if (branch->flags == PTQL_PID_SERVICE) {
@ -711,7 +708,7 @@ static int ptql_args_branch_init(ptql_parse_branch_t *parsed,
if (strtonum_failed(parsed->attr, end)) { if (strtonum_failed(parsed->attr, end)) {
/* conversion failed */ /* conversion failed */
return SIGAR_PTQL_MALFORMED_QUERY; return errno;
} }
} }
return SIGAR_OK; return SIGAR_OK;
@ -830,7 +827,7 @@ static int ptql_branch_init_port(ptql_parse_branch_t *parsed,
branch->flags = SIGAR_NETCONN_UDP; branch->flags = SIGAR_NETCONN_UDP;
} }
else { else {
return SIGAR_PTQL_MALFORMED_QUERY; return EINVAL;
} }
branch->data.ui32 = atoi(parsed->value); /*XXX*/ branch->data.ui32 = atoi(parsed->value); /*XXX*/
@ -1005,7 +1002,7 @@ static int ptql_branch_parse(char *query, ptql_parse_branch_t *branch)
branch->op_flags |= PTQL_OP_FLAG_PARENT; branch->op_flags |= PTQL_OP_FLAG_PARENT;
break; break;
default: default:
return SIGAR_PTQL_MALFORMED_QUERY; return EINVAL;
} }
++query; ++query;
@ -1142,7 +1139,7 @@ static int ptql_branch_add(ptql_parse_branch_t *parsed,
if (!is_set) { if (!is_set) {
branch->value.ui64 = strtoull(parsed->value, &ptr, 10); branch->value.ui64 = strtoull(parsed->value, &ptr, 10);
if (strtonum_failed(parsed->value, ptr)) { if (strtonum_failed(parsed->value, ptr)) {
return SIGAR_PTQL_MALFORMED_QUERY; return errno;
} }
} }
break; break;
@ -1151,7 +1148,7 @@ static int ptql_branch_add(ptql_parse_branch_t *parsed,
if (!is_set) { if (!is_set) {
branch->value.ui32 = strtoul(parsed->value, &ptr, 10); branch->value.ui32 = strtoul(parsed->value, &ptr, 10);
if (strtonum_failed(parsed->value, ptr)) { if (strtonum_failed(parsed->value, ptr)) {
return SIGAR_PTQL_MALFORMED_QUERY; return errno;
} }
} }
break; break;
@ -1160,7 +1157,7 @@ static int ptql_branch_add(ptql_parse_branch_t *parsed,
if (!is_set) { if (!is_set) {
branch->value.dbl = strtod(parsed->value, &ptr); branch->value.dbl = strtod(parsed->value, &ptr);
if (strtonum_failed(parsed->value, ptr)) { if (strtonum_failed(parsed->value, ptr)) {
return SIGAR_PTQL_MALFORMED_QUERY; return errno;
} }
} }
break; break;