From 5402c0bd4471882ecfa85b8d6952845a0ca124ee Mon Sep 17 00:00:00 2001 From: postgres Date: Sun, 20 Jul 2003 21:08:24 +0000 Subject: [PATCH] Settled on method for dealing with errors originating from within the R interpreter. Added global string "last_R_error_msg" to hold the result of geterrmessage(). An error in R now calls throw_r_error() with the result of geterrmessage() as its argument. throw_r_error() saves argument in last_R_error_msg. When the R call ends, PL/R will elog using last_R_error_msg whenever it is set, or a generic message when it is not. This works the same for both 7.3 and 7.4. --- pg_rsupport.c | 21 ++++++--------------- plr.c | 26 +++++++++++++++++++++----- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/pg_rsupport.c b/pg_rsupport.c index 30a9621c..b229c727 100755 --- a/pg_rsupport.c +++ b/pg_rsupport.c @@ -33,6 +33,7 @@ #include "plr.h" extern MemoryContext plr_SPI_context; +extern char *last_R_error_msg; static SEXP rpgsql_get_results(int ntuples, SPITupleTable *tuptable); @@ -593,30 +594,20 @@ plr_SPI_lastoid(void) return result; } -#ifdef PG_VERSION_73_COMPAT -/************************************************************************* - * working with postgres 7.3 compatible sources - *************************************************************************/ void throw_r_error(const char **msg) { - throw_pg_notice(msg); + if (msg && *msg) + last_R_error_msg = pstrdup(*msg); + else + last_R_error_msg = pstrdup("caught error calling R function"); } -#else +#ifndef PG_VERSION_73_COMPAT /************************************************************************* * working with postgres 7.4 compatible sources *************************************************************************/ -void -throw_r_error(const char **msg) -{ - if (msg && *msg) - elog(ERROR, "%s", *msg); - else - elog(ERROR, "%s", ""); -} - /* * error context callback to let us supply a call-stack traceback */ diff --git a/plr.c b/plr.c index 7c683ba4..3dfcb83e 100755 --- a/plr.c +++ b/plr.c @@ -37,6 +37,7 @@ */ MemoryContext plr_SPI_context; HTAB *plr_HashTable = (HTAB *) NULL; +char *last_R_error_msg = NULL; static bool plr_firstcall = true; static bool plr_interp_started = false; @@ -182,7 +183,10 @@ load_r_cmd(const char *cmd) PROTECT(cmdexpr = R_ParseVector(cmdSexp, -1, &status)); if (status != PARSE_OK) { UNPROTECT(2); - elog(ERROR, "plr: invalid R call: %s", cmd); + if (last_R_error_msg) + elog(ERROR, "%s", last_R_error_msg); + else + elog(ERROR, "R parse error: %s", cmd); } /* Loop is needed here as EXPSEXP may be of length > 1 */ @@ -190,7 +194,12 @@ load_r_cmd(const char *cmd) { ans = R_tryEval(VECTOR_ELT(cmdexpr, i), R_GlobalEnv, &status); if(status != 0) - elog(ERROR, "Caught an error calling R function"); + { + if (last_R_error_msg) + elog(ERROR, "%s", last_R_error_msg); + else + elog(ERROR, "%s", "caught error calling R function"); + } } UNPROTECT(2); @@ -836,7 +845,10 @@ plr_parse_func_body(const char *body) if (status != PARSE_OK) { UNPROTECT(2); - elog(ERROR, "plr: R parse error"); + if (last_R_error_msg) + elog(ERROR, "%s", last_R_error_msg); + else + elog(ERROR, "R parse error in function body"); } UNPROTECT(2); @@ -875,8 +887,12 @@ call_r_func(SEXP fun, SEXP rargs) UNPROTECT(1); if(errorOccurred) - elog(ERROR, "Caught an error calling R function"); - + { + if (last_R_error_msg) + elog(ERROR, "%s", last_R_error_msg); + else + elog(ERROR, "%s", "caught error calling R function"); + } return ans; }