Skip to content

Commit

Permalink
Settled on method for dealing with errors originating from within the
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
postgres committed Jul 20, 2003
1 parent d0be9df commit 5402c0b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
21 changes: 6 additions & 15 deletions pg_rsupport.c
Expand Up @@ -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);

Expand Down Expand Up @@ -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
*/
Expand Down
26 changes: 21 additions & 5 deletions plr.c
Expand Up @@ -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;
Expand Down Expand Up @@ -182,15 +183,23 @@ 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 */
for(i = 0; i < length(cmdexpr); i++)
{
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 5402c0b

Please sign in to comment.