Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0873b2d

Browse files
committed
libpq error message refactoring
libpq now contains a mix of error message strings that end with newlines and don't end with newlines, due to some newer code paths with new ways of passing errors around. This leads to confusion and mistakes both during development and translation. This adds new functions libpq_append_error() and libpq_append_conn_error() that encapsulate common code paths for producing error message strings. Notably, these functions append the newline, so that the string appearing in the code does not end with a newline. This makes (almost) all error message strings in libpq uniform in this regard (and also consistent with how we handle it outside of libpq code). (There are a few exceptions that are difficult to fit into this scheme, but they are only a few.) Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/flat/7c0232ef-7b44-68db-599d-b327d0640a77@enterprisedb.com
1 parent d627ce3 commit 0873b2d

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

src/interfaces/libpq/fe-misc.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,3 +1278,62 @@ libpq_ngettext(const char *msgid, const char *msgid_plural, unsigned long n)
12781278
}
12791279

12801280
#endif /* ENABLE_NLS */
1281+
1282+
1283+
/*
1284+
* Append a formatted string to the given buffer, after translating it. A
1285+
* newline is automatically appended; the format should not end with a
1286+
* newline.
1287+
*/
1288+
void
1289+
libpq_append_error(PQExpBuffer errorMessage, const char *fmt, ...)
1290+
{
1291+
int save_errno = errno;
1292+
bool done;
1293+
va_list args;
1294+
1295+
Assert(fmt[strlen(fmt) - 1] != '\n');
1296+
1297+
if (PQExpBufferBroken(errorMessage))
1298+
return; /* already failed */
1299+
1300+
/* Loop in case we have to retry after enlarging the buffer. */
1301+
do
1302+
{
1303+
errno = save_errno;
1304+
va_start(args, fmt);
1305+
done = appendPQExpBufferVA(errorMessage, libpq_gettext(fmt), args);
1306+
va_end(args);
1307+
} while (!done);
1308+
1309+
appendPQExpBufferChar(errorMessage, '\n');
1310+
}
1311+
1312+
/*
1313+
* Append a formatted string to the error message buffer of the given
1314+
* connection, after translating it. A newline is automatically appended; the
1315+
* format should not end with a newline.
1316+
*/
1317+
void
1318+
libpq_append_conn_error(PGconn *conn, const char *fmt, ...)
1319+
{
1320+
int save_errno = errno;
1321+
bool done;
1322+
va_list args;
1323+
1324+
Assert(fmt[strlen(fmt) - 1] != '\n');
1325+
1326+
if (PQExpBufferBroken(&conn->errorMessage))
1327+
return; /* already failed */
1328+
1329+
/* Loop in case we have to retry after enlarging the buffer. */
1330+
do
1331+
{
1332+
errno = save_errno;
1333+
va_start(args, fmt);
1334+
done = appendPQExpBufferVA(&conn->errorMessage, libpq_gettext(fmt), args);
1335+
va_end(args);
1336+
} while (!done);
1337+
1338+
appendPQExpBufferChar(&conn->errorMessage, '\n');
1339+
}

src/interfaces/libpq/libpq-int.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,9 @@ extern char *libpq_ngettext(const char *msgid, const char *msgid_plural, unsigne
887887
*/
888888
#undef _
889889

890+
extern void libpq_append_error(PQExpBuffer errorMessage, const char *fmt, ...) pg_attribute_printf(2, 3);
891+
extern void libpq_append_conn_error(PGconn *conn, const char *fmt, ...) pg_attribute_printf(2, 3);
892+
890893
/*
891894
* These macros are needed to let error-handling code be portable between
892895
* Unix and Windows. (ugh)

src/interfaces/libpq/nls.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# src/interfaces/libpq/nls.mk
22
CATALOG_NAME = libpq
33
GETTEXT_FILES = fe-auth.c fe-auth-scram.c fe-connect.c fe-exec.c fe-gssapi-common.c fe-lobj.c fe-misc.c fe-protocol3.c fe-secure.c fe-secure-common.c fe-secure-gssapi.c fe-secure-openssl.c win32.c ../../port/thread.c
4-
GETTEXT_TRIGGERS = libpq_gettext pqInternalNotice:2
5-
GETTEXT_FLAGS = libpq_gettext:1:pass-c-format pqInternalNotice:2:c-format
4+
GETTEXT_TRIGGERS = libpq_append_conn_error:2 \
5+
libpq_append_error:2 \
6+
libpq_gettext pqInternalNotice:2
7+
GETTEXT_FLAGS = libpq_append_conn_error:2:c-format \
8+
libpq_append_error:2:c-format \
9+
libpq_gettext:1:pass-c-format pqInternalNotice:2:c-format

src/interfaces/libpq/pqexpbuffer.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ static const char oom_buffer[1] = "";
4040
/* Need a char * for unconstify() compatibility */
4141
static const char *oom_buffer_ptr = oom_buffer;
4242

43-
static bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
44-
4543

4644
/*
4745
* markPQExpBufferBroken
@@ -292,7 +290,7 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
292290
* Caution: callers must be sure to preserve their entry-time errno
293291
* when looping, in case the fmt contains "%m".
294292
*/
295-
static bool
293+
bool
296294
appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args)
297295
{
298296
size_t avail;

src/interfaces/libpq/pqexpbuffer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,16 @@ extern void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) pg_attribute
157157
*/
158158
extern void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) pg_attribute_printf(2, 3);
159159

160+
/*------------------------
161+
* appendPQExpBufferVA
162+
* Attempt to format data and append it to str. Returns true if done
163+
* (either successful or hard failure), false if need to retry.
164+
*
165+
* Caution: callers must be sure to preserve their entry-time errno
166+
* when looping, in case the fmt contains "%m".
167+
*/
168+
extern bool appendPQExpBufferVA(PQExpBuffer str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
169+
160170
/*------------------------
161171
* appendPQExpBufferStr
162172
* Append the given string to a PQExpBuffer, allocating more space

0 commit comments

Comments
 (0)