From d0c38dd4debb5b3f4c6a0583a7c548fe7c88fad4 Mon Sep 17 00:00:00 2001
From: Abhishek Chanda <abhishek.becs@gmail.com>
Date: Sat, 12 Apr 2025 22:30:17 -0500
Subject: [PATCH v1] Add an error message when a given object is not found in a
 slash command

Currently, in a few cases we return an error of the form
Did not find any XXXX named YYYY while in some cases we
print a table with no rows. This patch changes a few of
the later to the former so that we have an uniform
interface.
---
 src/bin/psql/describe.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 1d08268393e..4e929504112 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -605,6 +605,14 @@ describeFunctions(const char *functypes, const char *func_pattern,
 	if (!res)
 		return false;
 
+	/* If there are no matching functions, print a message */
+	if (PQntuples(res) == 0 && func_pattern)
+	{
+		PQclear(res);
+		pg_log_error("Did not find any function named \"%s\".", func_pattern);
+		return true;
+	}
+
 	myopt.title = _("List of functions");
 	myopt.translate_header = true;
 	if (pset.sversion >= 90600)
@@ -721,6 +729,14 @@ describeTypes(const char *pattern, bool verbose, bool showSystem)
 	if (!res)
 		return false;
 
+	/* If there are no matching data types, print a message */
+	if (PQntuples(res) == 0 && pattern)
+	{
+		PQclear(res);
+		pg_log_error("Did not find any data type named \"%s\".", pattern);
+		return true;
+	}
+
 	myopt.title = _("List of data types");
 	myopt.translate_header = true;
 
@@ -3762,6 +3778,15 @@ describeRoles(const char *pattern, bool verbose, bool showSystem)
 		return false;
 
 	nrows = PQntuples(res);
+	
+	/* If there are no matching roles, print a message */
+	if (nrows == 0 && pattern)
+	{
+		PQclear(res);
+		pg_log_error("Did not find any role named \"%s\".", pattern);
+		return true;
+	}
+	
 	attr = pg_malloc0((nrows + 1) * sizeof(*attr));
 
 	printTableInit(&cont, &myopt, _("List of roles"), ncols, nrows);
@@ -5242,6 +5267,14 @@ listSchemas(const char *pattern, bool verbose, bool showSystem)
 	if (!res)
 		goto error_return;
 
+	/* If there are no matching schemas, print a message */
+	if (PQntuples(res) == 0 && pattern)
+	{
+		PQclear(res);
+		pg_log_error("Did not find any schema named \"%s\".", pattern);
+		return true;
+	}
+
 	myopt.title = _("List of schemas");
 	myopt.translate_header = true;
 
@@ -6213,6 +6246,14 @@ listExtensions(const char *pattern)
 	if (!res)
 		return false;
 
+	/* If there are no matching extensions, print a message */
+	if (PQntuples(res) == 0 && pattern)
+	{
+		PQclear(res);
+		pg_log_error("Did not find any extension named \"%s\".", pattern);
+		return true;
+	}
+
 	myopt.title = _("List of installed extensions");
 	myopt.translate_header = true;
 
-- 
2.49.0

