Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5681cde

Browse files
committed
Make some small improvements in the accuracy of plpgsql's error location
reports; inspired by the misleading CONTEXT lines shown in recent bug report from Stefan Kaltenbrunner. Also, allow statement-type names shown in these messages to be translated.
1 parent 17c8493 commit 5681cde

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.184 2007/01/28 16:15:49 tgl Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.185 2007/01/28 17:58:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -267,6 +267,8 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
267267
}
268268
}
269269

270+
estate.err_text = gettext_noop("during function entry");
271+
270272
/*
271273
* Set the magic variable FOUND to false
272274
*/
@@ -414,6 +416,8 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
414416
}
415417
}
416418

419+
estate.err_text = gettext_noop("during function exit");
420+
417421
/*
418422
* Let the instrumentation plugin peek at this function
419423
*/
@@ -608,6 +612,8 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
608612
CStringGetDatum(trigdata->tg_trigger->tgargs[i]));
609613
}
610614

615+
estate.err_text = gettext_noop("during function entry");
616+
611617
/*
612618
* Set the magic variable FOUND to false
613619
*/
@@ -644,6 +650,9 @@ plpgsql_exec_trigger(PLpgSQL_function *func,
644650
errmsg("control reached end of trigger procedure without RETURN")));
645651
}
646652

653+
estate.err_stmt = NULL;
654+
estate.err_text = gettext_noop("during function exit");
655+
647656
if (estate.retisset)
648657
ereport(ERROR,
649658
(errcode(ERRCODE_DATATYPE_MISMATCH),
@@ -711,30 +720,48 @@ plpgsql_exec_error_callback(void *arg)
711720
if (estate->err_text == raise_skip_msg)
712721
return;
713722

714-
if (estate->err_stmt != NULL)
715-
{
716-
/* translator: last %s is a plpgsql statement type name */
717-
errcontext("PL/pgSQL function \"%s\" line %d at %s",
718-
estate->err_func->fn_name,
719-
estate->err_stmt->lineno,
720-
plpgsql_stmt_typename(estate->err_stmt));
721-
}
722-
else if (estate->err_text != NULL)
723+
if (estate->err_text != NULL)
723724
{
724725
/*
725726
* We don't expend the cycles to run gettext() on err_text unless we
726727
* actually need it. Therefore, places that set up err_text should
727728
* use gettext_noop() to ensure the strings get recorded in the
728729
* message dictionary.
730+
*
731+
* If both err_text and err_stmt are set, use the err_text as
732+
* description, but report the err_stmt's line number. When
733+
* err_stmt is not set, we're in function entry/exit, or some such
734+
* place not attached to a specific line number.
729735
*/
730-
731-
/*
732-
* translator: last %s is a phrase such as "while storing call
733-
* arguments into local variables"
734-
*/
735-
errcontext("PL/pgSQL function \"%s\" %s",
736+
if (estate->err_stmt != NULL)
737+
{
738+
/*
739+
* translator: last %s is a phrase such as "during statement
740+
* block local variable initialization"
741+
*/
742+
errcontext("PL/pgSQL function \"%s\" line %d %s",
743+
estate->err_func->fn_name,
744+
estate->err_stmt->lineno,
745+
gettext(estate->err_text));
746+
}
747+
else
748+
{
749+
/*
750+
* translator: last %s is a phrase such as "while storing call
751+
* arguments into local variables"
752+
*/
753+
errcontext("PL/pgSQL function \"%s\" %s",
754+
estate->err_func->fn_name,
755+
gettext(estate->err_text));
756+
}
757+
}
758+
else if (estate->err_stmt != NULL)
759+
{
760+
/* translator: last %s is a plpgsql statement type name */
761+
errcontext("PL/pgSQL function \"%s\" line %d at %s",
736762
estate->err_func->fn_name,
737-
gettext(estate->err_text));
763+
estate->err_stmt->lineno,
764+
plpgsql_stmt_typename(estate->err_stmt));
738765
}
739766
else
740767
errcontext("PL/pgSQL function \"%s\"",
@@ -846,6 +873,8 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
846873
/*
847874
* First initialize all variables declared in this block
848875
*/
876+
estate->err_text = gettext_noop("during statement block local variable initialization");
877+
849878
for (i = 0; i < block->n_initvars; i++)
850879
{
851880
n = block->initvarnos[i];
@@ -915,6 +944,8 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
915944
EState *old_eval_estate = estate->eval_estate;
916945
long int old_eval_estate_simple_id = estate->eval_estate_simple_id;
917946

947+
estate->err_text = gettext_noop("during statement block entry");
948+
918949
BeginInternalSubTransaction(NULL);
919950
/* Want to run statements inside function's memory context */
920951
MemoryContextSwitchTo(oldcontext);
@@ -929,9 +960,13 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
929960
*/
930961
plpgsql_create_econtext(estate);
931962

963+
estate->err_text = NULL;
964+
932965
/* Run the block's statements */
933966
rc = exec_stmts(estate, block->body);
934967

968+
estate->err_text = gettext_noop("during statement block exit");
969+
935970
/* Commit the inner transaction, return to outer xact context */
936971
ReleaseCurrentSubTransaction();
937972
MemoryContextSwitchTo(oldcontext);
@@ -953,6 +988,8 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
953988
ErrorData *edata;
954989
ListCell *e;
955990

991+
estate->err_text = gettext_noop("during exception cleanup");
992+
956993
/* Save error info */
957994
MemoryContextSwitchTo(oldcontext);
958995
edata = CopyErrorData();
@@ -1004,6 +1041,8 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
10041041
errm_var->freeval = true;
10051042
errm_var->isnull = false;
10061043

1044+
estate->err_text = NULL;
1045+
10071046
rc = exec_stmts(estate, exception->action);
10081047

10091048
free_var(state_var);
@@ -1025,9 +1064,13 @@ exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
10251064
/*
10261065
* Just execute the statements in the block's body
10271066
*/
1067+
estate->err_text = NULL;
1068+
10281069
rc = exec_stmts(estate, block->body);
10291070
}
10301071

1072+
estate->err_text = NULL;
1073+
10311074
/*
10321075
* Handle the return code.
10331076
*/

src/pl/plpgsql/src/pl_funcs.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.56 2007/01/05 22:20:02 momjian Exp $
11+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.57 2007/01/28 17:58:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -426,43 +426,43 @@ plpgsql_stmt_typename(PLpgSQL_stmt *stmt)
426426
switch (stmt->cmd_type)
427427
{
428428
case PLPGSQL_STMT_BLOCK:
429-
return "block variables initialization";
429+
return _("statement block");
430430
case PLPGSQL_STMT_ASSIGN:
431-
return "assignment";
431+
return _("assignment");
432432
case PLPGSQL_STMT_IF:
433-
return "if";
433+
return _("if");
434434
case PLPGSQL_STMT_LOOP:
435-
return "loop";
435+
return _("loop");
436436
case PLPGSQL_STMT_WHILE:
437-
return "while";
437+
return _("while");
438438
case PLPGSQL_STMT_FORI:
439-
return "for with integer loopvar";
439+
return _("for with integer loop variable");
440440
case PLPGSQL_STMT_FORS:
441-
return "for over select rows";
441+
return _("for over select rows");
442442
case PLPGSQL_STMT_EXIT:
443-
return "exit";
443+
return _("exit");
444444
case PLPGSQL_STMT_RETURN:
445-
return "return";
445+
return _("return");
446446
case PLPGSQL_STMT_RETURN_NEXT:
447-
return "return next";
447+
return _("return next");
448448
case PLPGSQL_STMT_RAISE:
449-
return "raise";
449+
return _("raise");
450450
case PLPGSQL_STMT_EXECSQL:
451-
return "SQL statement";
451+
return _("SQL statement");
452452
case PLPGSQL_STMT_DYNEXECUTE:
453-
return "execute statement";
453+
return _("execute statement");
454454
case PLPGSQL_STMT_DYNFORS:
455-
return "for over execute statement";
455+
return _("for over execute statement");
456456
case PLPGSQL_STMT_GETDIAG:
457-
return "get diagnostics";
457+
return _("get diagnostics");
458458
case PLPGSQL_STMT_OPEN:
459-
return "open";
459+
return _("open");
460460
case PLPGSQL_STMT_FETCH:
461-
return "fetch";
461+
return _("fetch");
462462
case PLPGSQL_STMT_CLOSE:
463-
return "close";
463+
return _("close");
464464
case PLPGSQL_STMT_PERFORM:
465-
return "perform";
465+
return _("perform");
466466
}
467467

468468
return "unknown";

0 commit comments

Comments
 (0)