Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07d25a9

Browse files
committed
Improve error reporting in format()
Clarify invalid format conversion type error message and add hint. Author: Jim Nasby
1 parent a455878 commit 07d25a9

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

src/backend/utils/adt/varlena.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4788,7 +4788,8 @@ text_reverse(PG_FUNCTION_ARGS)
47884788
if (++(ptr) >= (end_ptr)) \
47894789
ereport(ERROR, \
47904790
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
4791-
errmsg("unterminated format specifier"))); \
4791+
errmsg("unterminated format() type specifier"), \
4792+
errhint("For a single \"%%\" use \"%%%%\"."))); \
47924793
} while (0)
47934794

47944795
/*
@@ -4920,8 +4921,9 @@ text_format(PG_FUNCTION_ARGS)
49204921
if (strchr("sIL", *cp) == NULL)
49214922
ereport(ERROR,
49224923
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4923-
errmsg("unrecognized conversion type specifier \"%c\"",
4924-
*cp)));
4924+
errmsg("unrecognized format() type specifier \"%c\"",
4925+
*cp),
4926+
errhint("For a single \"%%\" use \"%%%%\".")));
49254927

49264928
/* If indirect width was specified, get its value */
49274929
if (widthpos >= 0)
@@ -4932,7 +4934,7 @@ text_format(PG_FUNCTION_ARGS)
49324934
if (arg >= nargs)
49334935
ereport(ERROR,
49344936
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4935-
errmsg("too few arguments for format")));
4937+
errmsg("too few arguments for format()")));
49364938

49374939
/* Get the value and type of the selected argument */
49384940
if (!funcvariadic)
@@ -5040,8 +5042,9 @@ text_format(PG_FUNCTION_ARGS)
50405042
/* should not get here, because of previous check */
50415043
ereport(ERROR,
50425044
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
5043-
errmsg("unrecognized conversion type specifier \"%c\"",
5044-
*cp)));
5045+
errmsg("unrecognized format() type specifier \"%c\"",
5046+
*cp),
5047+
errhint("For a single \"%%\" use \"%%%%\".")));
50455048
break;
50465049
}
50475050
}

src/test/regress/expected/text.out

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ ERROR: too few arguments for format
211211
select format('Hello %s');
212212
ERROR: too few arguments for format
213213
select format('Hello %x', 20);
214-
ERROR: unrecognized conversion type specifier "x"
214+
ERROR: unrecognized format() type specifier "x"
215+
HINT: For a single "%" use "%%".
215216
-- check literal and sql identifiers
216217
select format('INSERT INTO %I VALUES(%L,%L)', 'mytab', 10, 'Hello');
217218
format
@@ -263,9 +264,11 @@ ERROR: format specifies argument 0, but arguments are numbered from 1
263264
select format('%*0$s', 'Hello');
264265
ERROR: format specifies argument 0, but arguments are numbered from 1
265266
select format('%1$', 1);
266-
ERROR: unterminated format specifier
267+
ERROR: unterminated format() type specifier
268+
HINT: For a single "%" use "%%".
267269
select format('%1$1', 1);
268-
ERROR: unterminated format specifier
270+
ERROR: unterminated format() type specifier
271+
HINT: For a single "%" use "%%".
269272
-- check mix of positional and ordered placeholders
270273
select format('Hello %s %1$s %s', 'World', 'Hello again');
271274
format

0 commit comments

Comments
 (0)