Improve new wording of libpq's connection failure messages.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 21 Jan 2021 21:10:18 +0000 (16:10 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 21 Jan 2021 21:10:18 +0000 (16:10 -0500)
"connection to server so-and-so failed:" seems clearer than the
previous wording "could not connect to so-and-so:" (introduced by
52a10224e), because the latter suggests a network-level connection
failure.  We're now prefixing this string to all types of connection
failures, for instance authentication failures; so we need wording
that doesn't imply a low-level error.

Per discussion with Robert Haas.

Discussion: https://postgr.es/m/CA+TgmobssJ6rS22dspWnu-oDxXevGmhMD8VcRBjmj-b9UDqRjw@mail.gmail.com

src/bin/pg_dump/t/002_pg_dump.pl
src/interfaces/ecpg/test/expected/connect-test5.stderr
src/interfaces/ecpg/test/pg_regress_ecpg.c
src/interfaces/libpq/fe-connect.c

index 2b501166b80f5ffad42de10e4af1d9daf31ee1d7..a9bbb80e63996d4eb9737d42bad654c99b9ced85 100644 (file)
@@ -3460,7 +3460,7 @@ foreach my $db (sort keys %create_sql)
 
 command_fails_like(
        [ 'pg_dump', '-p', "$port", 'qqq' ],
-       qr/pg_dump: error: connection to database "qqq" failed: could not connect to .*: FATAL:  database "qqq" does not exist/,
+       qr/pg_dump: error: connection to database "qqq" failed: connection to server .* failed: FATAL:  database "qqq" does not exist/,
        'connecting to a non-existent database');
 
 #########################################
index 4dbf2c0fc4626c9cb20343092fe8beecfc019dcd..db3cd9c2285db21cd2f94d55f37ffc3c6dc05d1a 100644 (file)
@@ -36,7 +36,7 @@
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT>  for user regress_ecpg_user2
 [NO_PID]: sqlca: code: 0, state: 00000
-[NO_PID]: ECPGconnect: could not open database: could not connect: FATAL:  database "regress_ecpg_user2" does not exist
+[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
 
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ecpg_finish: connection main closed
@@ -73,7 +73,7 @@
 [NO_PID]: sqlca: code: -220, state: 08003
 [NO_PID]: ECPGconnect: opening database <DEFAULT> on <DEFAULT> port <DEFAULT>  for user regress_ecpg_user2
 [NO_PID]: sqlca: code: 0, state: 00000
-[NO_PID]: ECPGconnect: could not open database: could not connect: FATAL:  database "regress_ecpg_user2" does not exist
+[NO_PID]: ECPGconnect: could not open database: connection to server failed: FATAL:  database "regress_ecpg_user2" does not exist
 
 [NO_PID]: sqlca: code: 0, state: 00000
 [NO_PID]: ecpg_finish: connection main closed
index 8d43fd65bad68ab6520035c7b36e1f2a197dc2b8..15f588a8023095dd4acadba9a57a503a67a2d472 100644 (file)
@@ -80,7 +80,7 @@ ecpg_filter_source(const char *sourcefile, const char *outfile)
 }
 
 /*
- * Remove the details of "could not connect to ...: " error messages
+ * Remove the details of connection failure error messages
  * in a test result file, since the target host/pathname and/or port
  * can vary.  Rewrite the result file in-place.
  *
@@ -113,15 +113,15 @@ ecpg_filter_stderr(const char *resultfile, const char *tmpfile)
 
        while (pg_get_line_buf(s, &linebuf))
        {
-               char       *p1 = strstr(linebuf.data, "could not connect to ");
+               char       *p1 = strstr(linebuf.data, "connection to server ");
 
                if (p1)
                {
-                       char       *p2 = strstr(p1, ": ");
+                       char       *p2 = strstr(p1, "failed: ");
 
                        if (p2)
                        {
-                               memmove(p1 + 17, p2, strlen(p2) + 1);
+                               memmove(p1 + 21, p2, strlen(p2) + 1);
                                /* we don't bother to fix up linebuf.len */
                        }
                }
index 2b78ed8ec3e34d2252a1dd988e5b643cf41e89eb..8ca0583aa908ddd09ce2ebb19d87996bd587789b 100644 (file)
@@ -1668,17 +1668,16 @@ getHostaddr(PGconn *conn, char *host_addr, int host_addr_len)
                host_addr[0] = '\0';
 }
 
-/* ----------
- * emitCouldNotConnect -
- * Speculatively append "could not connect to ...: " to conn->errorMessage
- * once we've identified the current connection target address.  This ensures
- * that any subsequent error message will be properly attributed to the
- * server we couldn't connect to.  conn->raddr must be valid, and the result
- * of getHostaddr() must be supplied.
- * ----------
+/*
+ * emitHostIdentityInfo -
+ * Speculatively append "connection to server so-and-so failed: " to
+ * conn->errorMessage once we've identified the current connection target
+ * address.  This ensures that any subsequent error message will be properly
+ * attributed to the server we couldn't connect to.  conn->raddr must be
+ * valid, and the result of getHostaddr() must be supplied.
  */
 static void
-emitCouldNotConnect(PGconn *conn, const char *host_addr)
+emitHostIdentityInfo(PGconn *conn, const char *host_addr)
 {
 #ifdef HAVE_UNIX_SOCKETS
        if (IS_AF_UNIX(conn->raddr.addr.ss_family))
@@ -1690,7 +1689,7 @@ emitCouldNotConnect(PGconn *conn, const char *host_addr)
                                                   service, sizeof(service),
                                                   NI_NUMERICSERV);
                appendPQExpBuffer(&conn->errorMessage,
-                                                 libpq_gettext("could not connect to socket \"%s\": "),
+                                                 libpq_gettext("connection to server on socket \"%s\" failed: "),
                                                  service);
        }
        else
@@ -1717,12 +1716,12 @@ emitCouldNotConnect(PGconn *conn, const char *host_addr)
                        host_addr[0] &&
                        strcmp(displayed_host, host_addr) != 0)
                        appendPQExpBuffer(&conn->errorMessage,
-                                                         libpq_gettext("could not connect to host \"%s\" (%s), port %s: "),
+                                                         libpq_gettext("connection to server at \"%s\" (%s), port %s failed: "),
                                                          displayed_host, host_addr,
                                                          displayed_port);
                else
                        appendPQExpBuffer(&conn->errorMessage,
-                                                         libpq_gettext("could not connect to host \"%s\", port %s: "),
+                                                         libpq_gettext("connection to server at \"%s\", port %s failed: "),
                                                          displayed_host,
                                                          displayed_port);
        }
@@ -2524,7 +2523,7 @@ keep_going:                                               /* We will come back to here until there is
                                                        conn->try_next_addr = true;
                                                        goto keep_going;
                                                }
-                                               emitCouldNotConnect(conn, host_addr);
+                                               emitHostIdentityInfo(conn, host_addr);
                                                appendPQExpBuffer(&conn->errorMessage,
                                                                                  libpq_gettext("could not create socket: %s\n"),
                                                                                  SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)));
@@ -2534,9 +2533,11 @@ keep_going:                                              /* We will come back to here until there is
                                        /*
                                         * Once we've identified a target address, all errors
                                         * except the preceding socket()-failure case should be
-                                        * prefixed with "could not connect to <target>: ".
+                                        * prefixed with host-identity information.  (If the
+                                        * connection succeeds, the contents of conn->errorMessage
+                                        * won't matter, so this is harmless.)
                                         */
-                                       emitCouldNotConnect(conn, host_addr);
+                                       emitHostIdentityInfo(conn, host_addr);
 
                                        /*
                                         * Select socket options: no delay of outgoing data for