From 91c5a8caaa61055959aa5fb68a00e5f690e39a34 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 31 Mar 2021 09:15:51 +0200 Subject: [PATCH] Add errhint_plural() function and make use of it Similar to existing errmsg_plural() and errdetail_plural(). Some errhint() calls hadn't received the proper plural treatment yet. --- doc/src/sgml/sources.sgml | 8 ++++++++ src/backend/parser/parse_func.c | 24 +++++++++++++++--------- src/backend/utils/error/elog.c | 23 +++++++++++++++++++++++ src/include/utils/elog.h | 3 +++ src/nls-global.mk | 4 ++-- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/doc/src/sgml/sources.sgml b/doc/src/sgml/sources.sgml index 62cf2fded4..3f2c40b750 100644 --- a/doc/src/sgml/sources.sgml +++ b/doc/src/sgml/sources.sgml @@ -282,6 +282,14 @@ ereport(ERROR, errmsg. + + + errhint_plural(const char *fmt_singular, const char *fmt_plural, + unsigned long n, ...) is like errhint, but with + support for various plural forms of the message. + For more information see . + + errcontext(const char *msg, ...) is not normally called diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index debef1d14f..baac089d68 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -417,9 +417,11 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, func_signature_string(funcname, nargs, argnames, actual_arg_types)), - errhint("There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", - NameListToString(funcname), - catDirectArgs, numDirectArgs), + errhint_plural("There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.", + "There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", + catDirectArgs, + NameListToString(funcname), + catDirectArgs, numDirectArgs), parser_errposition(pstate, location))); } else @@ -446,9 +448,11 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, func_signature_string(funcname, nargs, argnames, actual_arg_types)), - errhint("There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", - NameListToString(funcname), - catDirectArgs, numDirectArgs), + errhint_plural("There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.", + "There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", + catDirectArgs, + NameListToString(funcname), + catDirectArgs, numDirectArgs), parser_errposition(pstate, location))); } else @@ -485,9 +489,11 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, func_signature_string(funcname, nargs, argnames, actual_arg_types)), - errhint("There is an ordered-set aggregate %s, but it requires at least %d direct arguments.", - NameListToString(funcname), - catDirectArgs), + errhint_plural("There is an ordered-set aggregate %s, but it requires at least %d direct argument.", + "There is an ordered-set aggregate %s, but it requires at least %d direct arguments.", + catDirectArgs, + NameListToString(funcname), + catDirectArgs), parser_errposition(pstate, location))); } } diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index e729ebece7..9e4ea1b345 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -1166,6 +1166,29 @@ errhint(const char *fmt,...) } +/* + * errhint_plural --- add a hint error message text to the current error, + * with support for pluralization of the message text + */ +int +errhint_plural(const char *fmt_singular, const char *fmt_plural, + unsigned long n,...) +{ + ErrorData *edata = &errordata[errordata_stack_depth]; + MemoryContext oldcontext; + + recursion_depth++; + CHECK_STACK_DEPTH(); + oldcontext = MemoryContextSwitchTo(edata->assoc_context); + + EVALUATE_MESSAGE_PLURAL(edata->domain, hint, false); + + MemoryContextSwitchTo(oldcontext); + recursion_depth--; + return 0; /* return value does not matter */ +} + + /* * errcontext_msg --- add a context error message text to the current error * diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h index b59651289e..9acb57a27b 100644 --- a/src/include/utils/elog.h +++ b/src/include/utils/elog.h @@ -188,6 +188,9 @@ extern int errdetail_plural(const char *fmt_singular, const char *fmt_plural, extern int errhint(const char *fmt,...) pg_attribute_printf(1, 2); +extern int errhint_plural(const char *fmt_singular, const char *fmt_plural, + unsigned long n,...) pg_attribute_printf(1, 4) pg_attribute_printf(2, 4); + /* * errcontext() is typically called in error context callback functions, not * within an ereport() invocation. The callback function can be in a different diff --git a/src/nls-global.mk b/src/nls-global.mk index 30b40ffe2a..53129f0a04 100644 --- a/src/nls-global.mk +++ b/src/nls-global.mk @@ -57,7 +57,7 @@ BACKEND_COMMON_GETTEXT_TRIGGERS = \ $(FRONTEND_COMMON_GETTEXT_TRIGGERS) \ errmsg errmsg_plural:1,2 \ errdetail errdetail_log errdetail_plural:1,2 \ - errhint \ + errhint errhint_plural:1,2 \ errcontext \ XactLockTableWait:4 \ MultiXactIdWait:6 \ @@ -66,7 +66,7 @@ BACKEND_COMMON_GETTEXT_FLAGS = \ $(FRONTEND_COMMON_GETTEXT_FLAGS) \ errmsg:1:c-format errmsg_plural:1:c-format errmsg_plural:2:c-format \ errdetail:1:c-format errdetail_log:1:c-format errdetail_plural:1:c-format errdetail_plural:2:c-format \ - errhint:1:c-format \ + errhint:1:c-format errhint_plural:1:c-format errhint_plural:2:c-format \ errcontext:1:c-format FRONTEND_COMMON_GETTEXT_FILES = $(top_srcdir)/src/common/logging.c