@@ -683,13 +683,13 @@ errcode_for_socket_access(void)
683683 * to the edata field because the buffer might be considerably larger than
684684 * really necessary.
685685 */
686- #define EVALUATE_MESSAGE(targetfield, appendval, translateit) \
686+ #define EVALUATE_MESSAGE(domain, targetfield, appendval, translateit) \
687687 { \
688688 char *fmtbuf; \
689689 StringInfoData buf; \
690690 /* Internationalize the error format string */ \
691691 if (translateit && !in_error_recursion_trouble()) \
692- fmt = dgettext(edata-> domain, fmt); \
692+ fmt = dgettext(( domain) , fmt); \
693693 /* Expand %m in format string */ \
694694 fmtbuf = expand_fmt_string(fmt, edata); \
695695 initStringInfo(&buf); \
@@ -723,14 +723,14 @@ errcode_for_socket_access(void)
723723 * must be declared like "const char *fmt_singular, const char *fmt_plural,
724724 * unsigned long n, ...". Translation is assumed always wanted.
725725 */
726- #define EVALUATE_MESSAGE_PLURAL(targetfield, appendval) \
726+ #define EVALUATE_MESSAGE_PLURAL(domain, targetfield, appendval) \
727727 { \
728728 const char *fmt; \
729729 char *fmtbuf; \
730730 StringInfoData buf; \
731731 /* Internationalize the error format string */ \
732732 if (!in_error_recursion_trouble()) \
733- fmt = dngettext(edata-> domain, fmt_singular, fmt_plural, n); \
733+ fmt = dngettext(( domain) , fmt_singular, fmt_plural, n); \
734734 else \
735735 fmt = (n == 1 ? fmt_singular : fmt_plural); \
736736 /* Expand %m in format string */ \
@@ -781,7 +781,7 @@ errmsg(const char *fmt,...)
781781 CHECK_STACK_DEPTH();
782782 oldcontext = MemoryContextSwitchTo(ErrorContext);
783783
784- EVALUATE_MESSAGE(message, false, true);
784+ EVALUATE_MESSAGE(edata->domain, message, false, true);
785785
786786 MemoryContextSwitchTo(oldcontext);
787787 recursion_depth--;
@@ -810,7 +810,7 @@ errmsg_internal(const char *fmt,...)
810810 CHECK_STACK_DEPTH();
811811 oldcontext = MemoryContextSwitchTo(ErrorContext);
812812
813- EVALUATE_MESSAGE(message, false, false);
813+ EVALUATE_MESSAGE(edata->domain, message, false, false);
814814
815815 MemoryContextSwitchTo(oldcontext);
816816 recursion_depth--;
@@ -833,7 +833,7 @@ errmsg_plural(const char *fmt_singular, const char *fmt_plural,
833833 CHECK_STACK_DEPTH();
834834 oldcontext = MemoryContextSwitchTo(ErrorContext);
835835
836- EVALUATE_MESSAGE_PLURAL(message, false);
836+ EVALUATE_MESSAGE_PLURAL(edata->domain, message, false);
837837
838838 MemoryContextSwitchTo(oldcontext);
839839 recursion_depth--;
@@ -854,7 +854,7 @@ errdetail(const char *fmt,...)
854854 CHECK_STACK_DEPTH();
855855 oldcontext = MemoryContextSwitchTo(ErrorContext);
856856
857- EVALUATE_MESSAGE(detail, false, true);
857+ EVALUATE_MESSAGE(edata->domain, detail, false, true);
858858
859859 MemoryContextSwitchTo(oldcontext);
860860 recursion_depth--;
@@ -881,7 +881,7 @@ errdetail_internal(const char *fmt,...)
881881 CHECK_STACK_DEPTH();
882882 oldcontext = MemoryContextSwitchTo(ErrorContext);
883883
884- EVALUATE_MESSAGE(detail, false, false);
884+ EVALUATE_MESSAGE(edata->domain, detail, false, false);
885885
886886 MemoryContextSwitchTo(oldcontext);
887887 recursion_depth--;
@@ -902,7 +902,7 @@ errdetail_log(const char *fmt,...)
902902 CHECK_STACK_DEPTH();
903903 oldcontext = MemoryContextSwitchTo(ErrorContext);
904904
905- EVALUATE_MESSAGE(detail_log, false, true);
905+ EVALUATE_MESSAGE(edata->domain, detail_log, false, true);
906906
907907 MemoryContextSwitchTo(oldcontext);
908908 recursion_depth--;
@@ -925,7 +925,7 @@ errdetail_plural(const char *fmt_singular, const char *fmt_plural,
925925 CHECK_STACK_DEPTH();
926926 oldcontext = MemoryContextSwitchTo(ErrorContext);
927927
928- EVALUATE_MESSAGE_PLURAL(detail, false);
928+ EVALUATE_MESSAGE_PLURAL(edata->domain, detail, false);
929929
930930 MemoryContextSwitchTo(oldcontext);
931931 recursion_depth--;
@@ -946,7 +946,7 @@ errhint(const char *fmt,...)
946946 CHECK_STACK_DEPTH();
947947 oldcontext = MemoryContextSwitchTo(ErrorContext);
948948
949- EVALUATE_MESSAGE(hint, false, true);
949+ EVALUATE_MESSAGE(edata->domain, hint, false, true);
950950
951951 MemoryContextSwitchTo(oldcontext);
952952 recursion_depth--;
@@ -955,14 +955,14 @@ errhint(const char *fmt,...)
955955
956956
957957/*
958- * errcontext --- add a context error message text to the current error
958+ * errcontext_msg --- add a context error message text to the current error
959959 *
960960 * Unlike other cases, multiple calls are allowed to build up a stack of
961961 * context information. We assume earlier calls represent more-closely-nested
962962 * states.
963963 */
964964int
965- errcontext (const char *fmt,...)
965+ errcontext_msg (const char *fmt,...)
966966{
967967 ErrorData *edata = &errordata[errordata_stack_depth];
968968 MemoryContext oldcontext;
@@ -971,13 +971,35 @@ errcontext(const char *fmt,...)
971971 CHECK_STACK_DEPTH();
972972 oldcontext = MemoryContextSwitchTo(ErrorContext);
973973
974- EVALUATE_MESSAGE(context, true, true);
974+ EVALUATE_MESSAGE(edata->context_domain, context, true, true);
975975
976976 MemoryContextSwitchTo(oldcontext);
977977 recursion_depth--;
978978 return 0; /* return value does not matter */
979979}
980980
981+ /*
982+ * set_errcontext_domain --- set message domain to be used by errcontext()
983+ *
984+ * errcontext_msg() can be called from a different module than the original
985+ * ereport(), so we cannot use the message domain passed in errstart() to
986+ * translate it. Instead, each errcontext_msg() call should be preceded by
987+ * a set_errcontext_domain() call to specify the domain. This is usually
988+ * done transparently by the errcontext() macro.
989+ */
990+ int
991+ set_errcontext_domain(const char *domain)
992+ {
993+ ErrorData *edata = &errordata[errordata_stack_depth];
994+
995+ /* we don't bother incrementing recursion_depth */
996+ CHECK_STACK_DEPTH();
997+
998+ edata->context_domain = domain;
999+
1000+ return 0; /* return value does not matter */
1001+ }
1002+
9811003
9821004/*
9831005 * errhidestmt --- optionally suppress STATEMENT: field of log entry
@@ -1201,7 +1223,7 @@ elog_finish(int elevel, const char *fmt,...)
12011223 recursion_depth++;
12021224 oldcontext = MemoryContextSwitchTo(ErrorContext);
12031225
1204- EVALUATE_MESSAGE(message, false, false);
1226+ EVALUATE_MESSAGE(edata->domain, message, false, false);
12051227
12061228 MemoryContextSwitchTo(oldcontext);
12071229 recursion_depth--;
@@ -1260,7 +1282,7 @@ format_elog_string(const char *fmt,...)
12601282
12611283 oldcontext = MemoryContextSwitchTo(ErrorContext);
12621284
1263- EVALUATE_MESSAGE(message, false, true);
1285+ EVALUATE_MESSAGE(edata->domain, message, false, true);
12641286
12651287 MemoryContextSwitchTo(oldcontext);
12661288
0 commit comments