Skip to content

Commit 498ee9e

Browse files
committed
Refactor error messages to reduce duplication
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 9f8377f (16-era), in whose development thread there was no discussion on this point. Backpatch to 17.
1 parent d0c8cf2 commit 498ee9e

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

src/backend/commands/copy.c

+55-21
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,9 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
397397
if (!is_from)
398398
ereport(ERROR,
399399
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
400-
errmsg("COPY ON_ERROR cannot be used with COPY TO"),
400+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
401+
second %s is a COPY with direction, e.g. COPY TO */
402+
errmsg("COPY %s cannot be used with %s", "ON_ERROR", "COPY TO"),
401403
parser_errposition(pstate, def->location)));
402404

403405
/*
@@ -410,7 +412,8 @@ defGetCopyOnErrorChoice(DefElem *def, ParseState *pstate, bool is_from)
410412

411413
ereport(ERROR,
412414
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
413-
errmsg("COPY ON_ERROR \"%s\" not recognized", sval),
415+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
416+
errmsg("COPY %s \"%s\" not recognized", "ON_ERROR", sval),
414417
parser_errposition(pstate, def->location)));
415418
return COPY_ON_ERROR_STOP; /* keep compiler quiet */
416419
}
@@ -434,7 +437,8 @@ defGetCopyLogVerbosityChoice(DefElem *def, ParseState *pstate)
434437

435438
ereport(ERROR,
436439
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
437-
errmsg("COPY LOG_VERBOSITY \"%s\" not recognized", sval),
440+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR */
441+
errmsg("COPY %s \"%s\" not recognized", "LOG_VERBOSITY", sval),
438442
parser_errposition(pstate, def->location)));
439443
return COPY_LOG_VERBOSITY_DEFAULT; /* keep compiler quiet */
440444
}
@@ -647,17 +651,18 @@ ProcessCopyOptions(ParseState *pstate,
647651
if (opts_out->binary && opts_out->delim)
648652
ereport(ERROR,
649653
(errcode(ERRCODE_SYNTAX_ERROR),
650-
errmsg("cannot specify DELIMITER in BINARY mode")));
654+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
655+
errmsg("cannot specify %s in BINARY mode", "DELIMITER")));
651656

652657
if (opts_out->binary && opts_out->null_print)
653658
ereport(ERROR,
654659
(errcode(ERRCODE_SYNTAX_ERROR),
655-
errmsg("cannot specify NULL in BINARY mode")));
660+
errmsg("cannot specify %s in BINARY mode", "NULL")));
656661

657662
if (opts_out->binary && opts_out->default_print)
658663
ereport(ERROR,
659664
(errcode(ERRCODE_SYNTAX_ERROR),
660-
errmsg("cannot specify DEFAULT in BINARY mode")));
665+
errmsg("cannot specify %s in BINARY mode", "DEFAULT")));
661666

662667
if (opts_out->binary && opts_out->on_error != COPY_ON_ERROR_STOP)
663668
ereport(ERROR,
@@ -731,13 +736,15 @@ ProcessCopyOptions(ParseState *pstate,
731736
if (opts_out->binary && opts_out->header_line)
732737
ereport(ERROR,
733738
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
734-
errmsg("cannot specify HEADER in BINARY mode")));
739+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
740+
errmsg("cannot specify %s in BINARY mode", "HEADER")));
735741

736742
/* Check quote */
737743
if (!opts_out->csv_mode && opts_out->quote != NULL)
738744
ereport(ERROR,
739745
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
740-
errmsg("COPY QUOTE requires CSV mode")));
746+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
747+
errmsg("COPY %s requires CSV mode", "QUOTE")));
741748

742749
if (opts_out->csv_mode && strlen(opts_out->quote) != 1)
743750
ereport(ERROR,
@@ -753,7 +760,8 @@ ProcessCopyOptions(ParseState *pstate,
753760
if (!opts_out->csv_mode && opts_out->escape != NULL)
754761
ereport(ERROR,
755762
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
756-
errmsg("COPY ESCAPE requires CSV mode")));
763+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
764+
errmsg("COPY %s requires CSV mode", "ESCAPE")));
757765

758766
if (opts_out->csv_mode && strlen(opts_out->escape) != 1)
759767
ereport(ERROR,
@@ -764,71 +772,97 @@ ProcessCopyOptions(ParseState *pstate,
764772
if (!opts_out->csv_mode && (opts_out->force_quote || opts_out->force_quote_all))
765773
ereport(ERROR,
766774
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
767-
errmsg("COPY FORCE_QUOTE requires CSV mode")));
775+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
776+
errmsg("COPY %s requires CSV mode", "FORCE_QUOTE")));
768777
if ((opts_out->force_quote || opts_out->force_quote_all) && is_from)
769778
ereport(ERROR,
770779
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
771-
errmsg("COPY FORCE_QUOTE cannot be used with COPY FROM")));
780+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
781+
second %s is a COPY with direction, e.g. COPY TO */
782+
errmsg("COPY %s cannot be used with %s", "FORCE_QUOTE",
783+
"COPY FROM")));
772784

773785
/* Check force_notnull */
774786
if (!opts_out->csv_mode && opts_out->force_notnull != NIL)
775787
ereport(ERROR,
776788
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
777-
errmsg("COPY FORCE_NOT_NULL requires CSV mode")));
789+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
790+
errmsg("COPY %s requires CSV mode", "FORCE_NOT_NULL")));
778791
if (opts_out->force_notnull != NIL && !is_from)
779792
ereport(ERROR,
780793
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
781-
errmsg("COPY FORCE_NOT_NULL cannot be used with COPY TO")));
794+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
795+
second %s is a COPY with direction, e.g. COPY TO */
796+
errmsg("COPY %s cannot be used with %s", "FORCE_NOT_NULL",
797+
"COPY TO")));
782798

783799
/* Check force_null */
784800
if (!opts_out->csv_mode && opts_out->force_null != NIL)
785801
ereport(ERROR,
786802
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
787-
errmsg("COPY FORCE_NULL requires CSV mode")));
803+
/*- translator: %s is the name of a COPY option, e.g. ON_ERROR */
804+
errmsg("COPY %s requires CSV mode", "FORCE_NULL")));
788805

789806
if (opts_out->force_null != NIL && !is_from)
790807
ereport(ERROR,
791808
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
792-
errmsg("COPY FORCE_NULL cannot be used with COPY TO")));
809+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
810+
second %s is a COPY with direction, e.g. COPY TO */
811+
errmsg("COPY %s cannot be used with %s", "FORCE_NULL",
812+
"COPY TO")));
793813

794814
/* Don't allow the delimiter to appear in the null string. */
795815
if (strchr(opts_out->null_print, opts_out->delim[0]) != NULL)
796816
ereport(ERROR,
797817
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
798-
errmsg("COPY delimiter character must not appear in the NULL specification")));
818+
/*- translator: %s is the name of a COPY option, e.g. NULL */
819+
errmsg("COPY delimiter character must not appear in the %s specification",
820+
"NULL")));
799821

800822
/* Don't allow the CSV quote char to appear in the null string. */
801823
if (opts_out->csv_mode &&
802824
strchr(opts_out->null_print, opts_out->quote[0]) != NULL)
803825
ereport(ERROR,
804826
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
805-
errmsg("CSV quote character must not appear in the NULL specification")));
827+
/*- translator: %s is the name of a COPY option, e.g. NULL */
828+
errmsg("CSV quote character must not appear in the %s specification",
829+
"NULL")));
806830

807831
/* Check freeze */
808832
if (opts_out->freeze && !is_from)
809833
ereport(ERROR,
810834
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
811-
errmsg("COPY FREEZE cannot be used with COPY TO")));
835+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
836+
second %s is a COPY with direction, e.g. COPY TO */
837+
errmsg("COPY %s cannot be used with %s", "FREEZE",
838+
"COPY TO")));
812839

813840
if (opts_out->default_print)
814841
{
815842
if (!is_from)
816843
ereport(ERROR,
817844
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
818-
errmsg("COPY DEFAULT only available using COPY FROM")));
845+
/*- translator: first %s is the name of a COPY option, e.g. ON_ERROR,
846+
second %s is a COPY with direction, e.g. COPY TO */
847+
errmsg("COPY %s cannot be used with %s", "DEFAULT",
848+
"COPY TO")));
819849

820850
/* Don't allow the delimiter to appear in the default string. */
821851
if (strchr(opts_out->default_print, opts_out->delim[0]) != NULL)
822852
ereport(ERROR,
823853
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
824-
errmsg("COPY delimiter must not appear in the DEFAULT specification")));
854+
/*- translator: %s is the name of a COPY option, e.g. NULL */
855+
errmsg("COPY delimiter character must not appear in the %s specification",
856+
"DEFAULT")));
825857

826858
/* Don't allow the CSV quote char to appear in the default string. */
827859
if (opts_out->csv_mode &&
828860
strchr(opts_out->default_print, opts_out->quote[0]) != NULL)
829861
ereport(ERROR,
830862
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
831-
errmsg("CSV quote character must not appear in the DEFAULT specification")));
863+
/*- translator: %s is the name of a COPY option, e.g. NULL */
864+
errmsg("CSV quote character must not appear in the %s specification",
865+
"DEFAULT")));
832866

833867
/* Don't allow the NULL and DEFAULT string to be the same */
834868
if (opts_out->null_print_len == opts_out->default_print_len &&

src/backend/commands/copyfrom.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,9 @@ BeginCopyFrom(ParseState *pstate,
14541454
if (!list_member_int(cstate->attnumlist, attnum))
14551455
ereport(ERROR,
14561456
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1457-
errmsg("FORCE_NOT_NULL column \"%s\" not referenced by COPY",
1458-
NameStr(attr->attname))));
1457+
/*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
1458+
errmsg("%s column \"%s\" not referenced by COPY",
1459+
"FORCE_NOT_NULL", NameStr(attr->attname))));
14591460
cstate->opts.force_notnull_flags[attnum - 1] = true;
14601461
}
14611462
}
@@ -1496,8 +1497,9 @@ BeginCopyFrom(ParseState *pstate,
14961497
if (!list_member_int(cstate->attnumlist, attnum))
14971498
ereport(ERROR,
14981499
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
1499-
errmsg("FORCE_NULL column \"%s\" not referenced by COPY",
1500-
NameStr(attr->attname))));
1500+
/*- translator: first %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
1501+
errmsg("%s column \"%s\" not referenced by COPY",
1502+
"FORCE_NULL", NameStr(attr->attname))));
15011503
cstate->opts.force_null_flags[attnum - 1] = true;
15021504
}
15031505
}

src/backend/commands/copyto.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,9 @@ BeginCopyTo(ParseState *pstate,
593593
if (!list_member_int(cstate->attnumlist, attnum))
594594
ereport(ERROR,
595595
(errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
596-
errmsg("FORCE_QUOTE column \"%s\" not referenced by COPY",
597-
NameStr(attr->attname))));
596+
/*- translator: %s is the name of a COPY option, e.g. FORCE_NOT_NULL */
597+
errmsg("%s column \"%s\" not referenced by COPY",
598+
"FORCE_QUOTE", NameStr(attr->attname))));
598599
cstate->opts.force_quote_flags[attnum - 1] = true;
599600
}
600601
}

src/test/regress/expected/copy2.out

+2-2
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ copy copy_default from stdin with (default E'\r');
844844
ERROR: COPY default representation cannot use newline or carriage return
845845
-- DELIMITER cannot appear in DEFAULT spec
846846
copy copy_default from stdin with (delimiter ';', default 'test;test');
847-
ERROR: COPY delimiter must not appear in the DEFAULT specification
847+
ERROR: COPY delimiter character must not appear in the DEFAULT specification
848848
-- CSV quote cannot appear in DEFAULT spec
849849
copy copy_default from stdin with (format csv, quote '"', default 'test"test');
850850
ERROR: CSV quote character must not appear in the DEFAULT specification
@@ -904,4 +904,4 @@ select id, text_value, ts_value from copy_default;
904904
truncate copy_default;
905905
-- DEFAULT cannot be used in COPY TO
906906
copy (select 1 as test) TO stdout with (default '\D');
907-
ERROR: COPY DEFAULT only available using COPY FROM
907+
ERROR: COPY DEFAULT cannot be used with COPY TO

0 commit comments

Comments
 (0)