Refactor error messages to reduce duplication
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 8 Aug 2024 19:17:11 +0000 (15:17 -0400)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Thu, 8 Aug 2024 19:17:11 +0000 (15:17 -0400)
I also took the liberty of changing

errmsg("COPY DEFAULT only available using COPY FROM")
to
errmsg("COPY %s cannot be used with %s", "DEFAULT", "COPY TO")

because the original wording is unlike all other messages that indicate
option incompatibility.  This message was added by commit 9f8377f7a279
(16-era), in whose development thread there was no discussion on this
point.

Backpatch to 17.

src/backend/commands/copy.c
src/backend/commands/copyfrom.c
src/backend/commands/copyto.c
src/test/regress/expected/copy2.out

index df7a4a21c9458ce29af63e58e19bd362293a56a0..3bb579a3a4464f3efd0af8d27e0215210104d902 100644 (file)
@@ -397,7 +397,9 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
    if (!is_from)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("COPY ON_ERROR cannot be used with COPY TO"),
+       /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+        second %s is a COPY with direction, e.g. COPY TO */
+                errmsg("COPY %s cannot be used with %s", "ON_ERROR", "COPY TO"),
                 parser_errposition(pstate, def->location)));
 
    /*
@@ -410,7 +412,8 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
 
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-            errmsg("COPY ON_ERROR \"%s\" not recognized", sval),
+   /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
+            errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
             parser_errposition(pstate, def->location)));
    return COPY_ON_ERROR_STOP;  /* keep compiler quiet */
 }
@@ -434,7 +437,8 @@ defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
 
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-            errmsg("COPY LOG_VERBOSITY \"%s\" not recognized", sval),
+   /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
+            errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
             parser_errposition(pstate, def->location)));
    return COPY_LOG_VERBOSITY_DEFAULT;  /* keep compiler quiet */
 }
@@ -647,17 +651,18 @@ ProcessCopyOptions(ParseState *pstate,
    if (opts_out->binary && opts_out->delim)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
-                errmsg("cannot specify DELIMITER in BINARY mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("cannot specify %s in BINARY mode", "DELIMITER")));
 
    if (opts_out->binary && opts_out->null_print)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
-                errmsg("cannot specify NULL in BINARY mode")));
+                errmsg("cannot specify %s in BINARY mode", "NULL")));
 
    if (opts_out->binary && opts_out->default_print)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
-                errmsg("cannot specify DEFAULT in BINARY mode")));
+                errmsg("cannot specify %s in BINARY mode", "DEFAULT")));
 
    if (opts_out->binary && opts_out->on_error != COPY_ON_ERROR_STOP)
        ereport(ERROR,
@@ -731,13 +736,15 @@ ProcessCopyOptions(ParseState *pstate,
    if (opts_out->binary && opts_out->header_line)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("cannot specify HEADER in BINARY mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("cannot specify %s in BINARY mode", "HEADER")));
 
    /* Check quote */
    if (!opts_out->csv_mode && opts_out->quote != NULL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY QUOTE requires CSV mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("COPY %s requires CSV mode", "QUOTE")));
 
    if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
        ereport(ERROR,
@@ -753,7 +760,8 @@ ProcessCopyOptions(ParseState *pstate,
    if (!opts_out->csv_mode && opts_out->escape != NULL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY ESCAPE requires CSV mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("COPY %s requires CSV mode", "ESCAPE")));
 
    if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
        ereport(ERROR,
@@ -764,71 +772,97 @@ ProcessCopyOptions(ParseState *pstate,
    if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY FORCE_QUOTE requires CSV mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("COPY %s requires CSV mode", "FORCE_QUOTE")));
    if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY FORCE_QUOTE cannot be used with COPY FROM")));
+       /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+        second %s is a COPY with direction, e.g. COPY TO */
+                errmsg("COPY %s cannot be used with %s", "FORCE_QUOTE",
+                       "COPY FROM")));
 
    /* Check force_notnull */
    if (!opts_out->csv_mode && opts_out->force_notnull != NIL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY FORCE_NOT_NULL requires CSV mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
    if (opts_out->force_notnull != NIL && !is_from)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("COPY FORCE_NOT_NULL cannot be used with COPY TO")));
+       /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+        second %s is a COPY with direction, e.g. COPY TO */
+                errmsg("COPY %s cannot be used with %s", "FORCE_NOT_NULL",
+                       "COPY TO")));
 
    /* Check force_null */
    if (!opts_out->csv_mode && opts_out->force_null != NIL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                errmsg("COPY FORCE_NULL requires CSV mode")));
+       /*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
+                errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
 
    if (opts_out->force_null != NIL && !is_from)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("COPY FORCE_NULL cannot be used with COPY TO")));
+       /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+        second %s is a COPY with direction, e.g. COPY TO */
+                errmsg("COPY %s cannot be used with %s", "FORCE_NULL",
+                       "COPY TO")));
 
    /* Don't allow the delimiter to appear in the null string. */
    if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("COPY delimiter character must not appear in the NULL specification")));
+       /*- translator: %s is the name of a COPY option, e.g. NULL */
+                errmsg("COPY delimiter character must not appear in the %s specification",
+                       "NULL")));
 
    /* Don't allow the CSV quote char to appear in the null string. */
    if (opts_out->csv_mode &&
        strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("CSV quote character must not appear in the NULL specification")));
+       /*- translator: %s is the name of a COPY option, e.g. NULL */
+                errmsg("CSV quote character must not appear in the %s specification",
+                       "NULL")));
 
    /* Check freeze */
    if (opts_out->freeze && !is_from)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                errmsg("COPY FREEZE cannot be used with COPY TO")));
+       /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+        second %s is a COPY with direction, e.g. COPY TO */
+                errmsg("COPY %s cannot be used with %s", "FREEZE",
+                       "COPY TO")));
 
    if (opts_out->default_print)
    {
        if (!is_from)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                    errmsg("COPY DEFAULT only available using COPY FROM")));
+           /*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
+            second %s is a COPY with direction, e.g. COPY TO */
+                    errmsg("COPY %s cannot be used with %s", "DEFAULT",
+                           "COPY TO")));
 
        /* Don't allow the delimiter to appear in the default string. */
        if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                    errmsg("COPY delimiter must not appear in the DEFAULT specification")));
+           /*- translator: %s is the name of a COPY option, e.g. NULL */
+                    errmsg("COPY delimiter character must not appear in the %s specification",
+                           "DEFAULT")));
 
        /* Don't allow the CSV quote char to appear in the default string. */
        if (opts_out->csv_mode &&
            strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                    errmsg("CSV quote character must not appear in the DEFAULT specification")));
+           /*- translator: %s is the name of a COPY option, e.g. NULL */
+                    errmsg("CSV quote character must not appear in the %s specification",
+                           "DEFAULT")));
 
        /* Don't allow the NULL and DEFAULT string to be the same */
        if (opts_out->null_print_len == opts_out->default_print_len &&
index c42a5621d5a6b4d1545d54a8801a1dfd50b4b0b4..ca85270be6d35e27859eb8311a393ac593a94963 100644 (file)
@@ -1454,8 +1454,9 @@ BeginCopyFrom(ParseState *pstate,
            if (!list_member_int(cstate->attnumlist, attnum))
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                        errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
-                               NameStr(attr->attname))));
+               /*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
+                        errmsg("%s column \"%s\" not referenced by COPY",
+                               "FORCE_NOT_NULL", NameStr(attr->attname))));
            cstate->opts.force_notnull_flags[attnum - 1] = true;
        }
    }
@@ -1496,8 +1497,9 @@ BeginCopyFrom(ParseState *pstate,
            if (!list_member_int(cstate->attnumlist, attnum))
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                        errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
-                               NameStr(attr->attname))));
+               /*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
+                        errmsg("%s column \"%s\" not referenced by COPY",
+                               "FORCE_NULL", NameStr(attr->attname))));
            cstate->opts.force_null_flags[attnum - 1] = true;
        }
    }
index ae8b2e36d72db2d3f3d631736c1cf0b0c168bd62..eb1d3d8fbb5a056e37ac0ec794dc62a08d26f849 100644 (file)
@@ -593,8 +593,9 @@ BeginCopyTo(ParseState *pstate,
            if (!list_member_int(cstate->attnumlist, attnum))
                ereport(ERROR,
                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                        errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
-                               NameStr(attr->attname))));
+               /*- translator: %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
+                        errmsg("%s column \"%s\" not referenced by COPY",
+                               "FORCE_QUOTE", NameStr(attr->attname))));
            cstate->opts.force_quote_flags[attnum - 1] = true;
        }
    }
index 931542f2689c7cf578677d27c7ee7b5a81869b39..e913f683a684e826b9061f0b85fc76ac5553b139 100644 (file)
@@ -844,7 +844,7 @@ copy copy_default from stdin with (default E'\r');
 ERROR:  COPY default representation cannot use newline or carriage return
 -- DELIMITER cannot appear in DEFAULT spec
 copy copy_default from stdin with (delimiter ';', default 'test;test');
-ERROR:  COPY delimiter must not appear in the DEFAULT specification
+ERROR:  COPY delimiter character must not appear in the DEFAULT specification
 -- CSV quote cannot appear in DEFAULT spec
 copy copy_default from stdin with (format csv, quote '"', default 'test"test');
 ERROR:  CSV quote character must not appear in the DEFAULT specification
@@ -904,4 +904,4 @@ select id, text_value, ts_value from copy_default;
 truncate copy_default;
 -- DEFAULT cannot be used in COPY TO
 copy (select 1 as test) TO stdout with (default '\D');
-ERROR:  COPY DEFAULT only available using COPY FROM
+ERROR:  COPY DEFAULT cannot be used with COPY TO