From bb0e3011f8670cefc7b4ef667d890c9adb7556db Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 28 Nov 2007 21:56:30 +0000
Subject: [PATCH] Make a cleanup pass over error reports in tsearch code.  Use
 ereport for user-facing errors, fix some poor choices of errcode, adhere to
 message style guide.

---
 src/backend/commands/tsearchcmds.c      |  7 ++---
 src/backend/tsearch/dict_thesaurus.c    | 34 ++++++++++++++++++-------
 src/backend/tsearch/spell.c             |  4 +--
 src/backend/tsearch/ts_parse.c          | 18 ++++++++-----
 src/backend/utils/adt/tsquery.c         | 29 +++++++++++----------
 src/backend/utils/adt/tsquery_cleanup.c |  5 ++--
 src/backend/utils/adt/tsvector.c        |  8 +++---
 src/backend/utils/adt/tsvector_op.c     |  8 +++---
 src/backend/utils/adt/tsvector_parser.c |  5 ++--
 9 files changed, 71 insertions(+), 47 deletions(-)

diff --git a/src/backend/commands/tsearchcmds.c b/src/backend/commands/tsearchcmds.c
index 30116cd01d6..308024ed3c9 100644
--- a/src/backend/commands/tsearchcmds.c
+++ b/src/backend/commands/tsearchcmds.c
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/tsearchcmds.c,v 1.7 2007/11/15 22:25:15 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/tsearchcmds.c,v 1.8 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -95,7 +95,8 @@ get_ts_parser_func(DefElem *defel, int attnum)
 			break;
 		default:
 			/* should not be here */
-			elog(ERROR, "unknown attribute for text search parser: %d", attnum);
+			elog(ERROR, "unrecognized attribute for text search parser: %d",
+				 attnum);
 			nargs = 0;			/* keep compiler quiet */
 	}
 
@@ -895,7 +896,7 @@ get_ts_template_func(DefElem *defel, int attnum)
 			break;
 		default:
 			/* should not be here */
-			elog(ERROR, "unknown attribute for text search template: %d",
+			elog(ERROR, "unrecognized attribute for text search template: %d",
 				 attnum);
 			nargs = 0;			/* keep compiler quiet */
 	}
diff --git a/src/backend/tsearch/dict_thesaurus.c b/src/backend/tsearch/dict_thesaurus.c
index 5bf01c1b508..fcac2a61099 100644
--- a/src/backend/tsearch/dict_thesaurus.c
+++ b/src/backend/tsearch/dict_thesaurus.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tsearch/dict_thesaurus.c,v 1.9 2007/11/28 04:24:38 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tsearch/dict_thesaurus.c,v 1.10 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -423,11 +423,18 @@ compileTheLexeme(DictThesaurus *d)
 													 PointerGetDatum(NULL)));
 
 			if (!ptr)
-				elog(ERROR, "thesaurus word-sample \"%s\" isn't recognized by subdictionary (rule %d)",
-					 d->wrds[i].lexeme, d->wrds[i].entries->idsubst + 1);
+				ereport(ERROR,
+						(errcode(ERRCODE_CONFIG_FILE_ERROR),
+						 errmsg("thesaurus sample word \"%s\" isn't recognized by subdictionary (rule %d)",
+								d->wrds[i].lexeme,
+								d->wrds[i].entries->idsubst + 1)));
 			else if (!(ptr->lexeme))
-				elog(ERROR, "thesaurus word-sample \"%s\" is recognized as stop-word, use \"?\" for stop words instead (rule %d)",
-					 d->wrds[i].lexeme, d->wrds[i].entries->idsubst + 1);
+				ereport(ERROR,
+						(errcode(ERRCODE_CONFIG_FILE_ERROR),
+						 errmsg("thesaurus sample word \"%s\" is a stop word (rule %d)",
+								d->wrds[i].lexeme,
+								d->wrds[i].entries->idsubst + 1),
+						 errhint("Use \"?\" to represent a stop word within a sample phrase.")));
 			else
 			{
 				while (ptr->lexeme)
@@ -570,11 +577,17 @@ compileTheSubstitute(DictThesaurus *d)
 			}
 			else if (lexized)
 			{
-				elog(ERROR, "thesaurus word \"%s\" in substitution is a stop-word (rule %d)", inptr->lexeme, i + 1);
+				ereport(ERROR,
+						(errcode(ERRCODE_CONFIG_FILE_ERROR),
+						 errmsg("thesaurus substitute word \"%s\" is a stop word (rule %d)",
+								inptr->lexeme, i + 1)));
 			}
 			else
 			{
-				elog(ERROR, "thesaurus word \"%s\" in substitution isn't recognized (rule %d)", inptr->lexeme, i + 1);
+				ereport(ERROR,
+						(errcode(ERRCODE_CONFIG_FILE_ERROR),
+						 errmsg("thesaurus substitute word \"%s\" isn't recognized by subdictionary (rule %d)",
+								inptr->lexeme, i + 1)));
 			}
 
 			if (inptr->lexeme)
@@ -583,7 +596,10 @@ compileTheSubstitute(DictThesaurus *d)
 		}
 
 		if (outptr == d->subst[i].res)
-			elog(ERROR, "all words in thesaurus substitution are stop words (rule %d)", i + 1);
+			ereport(ERROR,
+					(errcode(ERRCODE_CONFIG_FILE_ERROR),
+					 errmsg("thesaurus substitute phrase is empty (rule %d)",
+							i + 1)));
 
 		d->subst[i].reslen = outptr - d->subst[i].res;
 
@@ -794,7 +810,7 @@ thesaurus_lexize(PG_FUNCTION_ARGS)
 	uint16		curpos = 0;
 	bool		moreres = false;
 
-	if (PG_NARGS() < 4 || dstate == NULL)
+	if (PG_NARGS() != 4 || dstate == NULL)
 		elog(ERROR, "forbidden call of thesaurus or nested call");
 
 	if (dstate->isend)
diff --git a/src/backend/tsearch/spell.c b/src/backend/tsearch/spell.c
index fdb606d213e..aa5048277c1 100644
--- a/src/backend/tsearch/spell.c
+++ b/src/backend/tsearch/spell.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tsearch/spell.c,v 1.7 2007/11/15 22:25:16 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tsearch/spell.c,v 1.8 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -501,7 +501,7 @@ parse_affentry(char *str, char *mask, char *find, char *repl,
 							  lineno, filename)));
 		}
 		else
-			elog(ERROR, "unknown state in parse_affentry: %d", state);
+			elog(ERROR, "unrecognized state in parse_affentry: %d", state);
 
 		str += pg_mblen(str);
 	}
diff --git a/src/backend/tsearch/ts_parse.c b/src/backend/tsearch/ts_parse.c
index ef54af3935f..53ea6010d27 100644
--- a/src/backend/tsearch/ts_parse.c
+++ b/src/backend/tsearch/ts_parse.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/tsearch/ts_parse.c,v 1.5 2007/11/15 22:25:16 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/tsearch/ts_parse.c,v 1.6 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -380,15 +380,17 @@ parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
 		{
 #ifdef IGNORE_LONGLEXEME
 			ereport(NOTICE,
-					(errcode(ERRCODE_SYNTAX_ERROR),
+					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 					 errmsg("word is too long to be indexed"),
 					 errdetail("Words longer than %d characters are ignored.",
 							   MAXSTRLEN)));
 			continue;
 #else
 			ereport(ERROR,
-					(errcode(ERRCODE_SYNTAX_ERROR),
-					 errmsg("word is too long to be indexed")));
+					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+					 errmsg("word is too long to be indexed"),
+					 errdetail("Words longer than %d characters are ignored.",
+							   MAXSTRLEN)));
 #endif
 		}
 
@@ -547,15 +549,17 @@ hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int bu
 		{
 #ifdef IGNORE_LONGLEXEME
 			ereport(NOTICE,
-					(errcode(ERRCODE_SYNTAX_ERROR),
+					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 					 errmsg("word is too long to be indexed"),
 					 errdetail("Words longer than %d characters are ignored.",
 							   MAXSTRLEN)));
 			continue;
 #else
 			ereport(ERROR,
-					(errcode(ERRCODE_SYNTAX_ERROR),
-					 errmsg("word is too long to be indexed")));
+					(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+					 errmsg("word is too long to be indexed"),
+					 errdetail("Words longer than %d characters are ignored.",
+							   MAXSTRLEN)));
 #endif
 		}
 
diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c
index 509998c5102..53ec74289a9 100644
--- a/src/backend/utils/adt/tsquery.c
+++ b/src/backend/utils/adt/tsquery.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsquery.c,v 1.11 2007/11/16 15:05:59 teodor Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsquery.c,v 1.12 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -237,12 +237,12 @@ pushValue_internal(TSQueryParserState state, pg_crc32 valcrc, int distance, int
 
 	if (distance >= MAXSTRPOS)
 		ereport(ERROR,
-				(errcode(ERRCODE_SYNTAX_ERROR),
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 				 errmsg("value is too big in tsquery: \"%s\"",
 						state->buffer)));
 	if (lenval >= MAXSTRLEN)
 		ereport(ERROR,
-				(errcode(ERRCODE_SYNTAX_ERROR),
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 				 errmsg("operand is too long in tsquery: \"%s\"",
 						state->buffer)));
 
@@ -269,7 +269,7 @@ pushValue(TSQueryParserState state, char *strval, int lenval, int2 weight)
 
 	if (lenval >= MAXSTRLEN)
 		ereport(ERROR,
-				(errcode(ERRCODE_SYNTAX_ERROR),
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
 				 errmsg("word is too long in tsquery: \"%s\"",
 						state->buffer)));
 
@@ -396,7 +396,7 @@ findoprnd_recurse(QueryItem *ptr, uint32 *pos, int nnodes)
 	check_stack_depth();
 
 	if (*pos >= nnodes)
-		elog(ERROR, "malformed tsquery; operand not found");
+		elog(ERROR, "malformed tsquery: operand not found");
 
 	if (ptr[*pos].type == QI_VAL ||
 		ptr[*pos].type == QI_VALSTOP)	/* need to handle VALSTOP here, they
@@ -443,7 +443,7 @@ findoprnd(QueryItem *ptr, int size)
 	findoprnd_recurse(ptr, &pos, size);
 
 	if (pos != size)
-		elog(ERROR, "malformed tsquery; extra nodes");
+		elog(ERROR, "malformed tsquery: extra nodes");
 }
 
 
@@ -531,7 +531,7 @@ parse_tsquery(char *buf,
 				memcpy(&ptr[i], item, sizeof(QueryOperator));
 				break;
 			default:
-				elog(ERROR, "unknown QueryItem type %d", item->type);
+				elog(ERROR, "unrecognized QueryItem type: %d", item->type);
 		}
 		i++;
 	}
@@ -718,7 +718,7 @@ infix(INFIX *in, bool first)
 				break;
 			default:
 				/* OP_NOT is handled in above if-branch */
-				elog(ERROR, "unexpected operator type %d", op);
+				elog(ERROR, "unrecognized operator type: %d", op);
 		}
 		in->cur = strchr(in->cur, '\0');
 		pfree(nrm.buf);
@@ -798,7 +798,7 @@ tsquerysend(PG_FUNCTION_ARGS)
 				pq_sendint(&buf, item->operator.oper, sizeof(item->operator.oper));
 				break;
 			default:
-				elog(ERROR, "unknown tsquery node type %d", item->type);
+				elog(ERROR, "unrecognized tsquery node type: %d", item->type);
 		}
 		item++;
 	}
@@ -853,13 +853,13 @@ tsqueryrecv(PG_FUNCTION_ARGS)
 			/* Sanity checks */
 
 			if (weight > 0xF)
-				elog(ERROR, "invalid tsquery; invalid weight bitmap");
+				elog(ERROR, "invalid tsquery: invalid weight bitmap");
 
 			if (val_len > MAXSTRLEN)
-				elog(ERROR, "invalid tsquery; operand too long");
+				elog(ERROR, "invalid tsquery: operand too long");
 
 			if (datalen > MAXSTRPOS)
-				elog(ERROR, "invalid tsquery; total operand length exceeded");
+				elog(ERROR, "invalid tsquery: total operand length exceeded");
 
 			/* Looks valid. */
 
@@ -886,14 +886,15 @@ tsqueryrecv(PG_FUNCTION_ARGS)
 
 			oper = (int8) pq_getmsgint(buf, sizeof(int8));
 			if (oper != OP_NOT && oper != OP_OR && oper != OP_AND)
-				elog(ERROR, "invalid tsquery; unknown operator type %d", (int) oper);
+				elog(ERROR, "invalid tsquery: unrecognized operator type %d",
+					 (int) oper);
 			if (i == size - 1)
 				elog(ERROR, "invalid pointer to right operand");
 
 			item->operator.oper = oper;
 		}
 		else
-			elog(ERROR, "unknown tsquery node type %d", item->type);
+			elog(ERROR, "unrecognized tsquery node type: %d", item->type);
 
 		item++;
 	}
diff --git a/src/backend/utils/adt/tsquery_cleanup.c b/src/backend/utils/adt/tsquery_cleanup.c
index c395a1f2c2a..c58cce8f5e7 100644
--- a/src/backend/utils/adt/tsquery_cleanup.c
+++ b/src/backend/utils/adt/tsquery_cleanup.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_cleanup.c,v 1.7 2007/11/15 22:25:16 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsquery_cleanup.c,v 1.8 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -281,7 +281,8 @@ clean_fakeval(QueryItem *ptr, int *len)
 	resroot = clean_fakeval_intree(root, &result);
 	if (result != V_UNKNOWN)
 	{
-		elog(NOTICE, "query contains only stopword(s) or doesn't contain lexeme(s), ignored");
+		ereport(NOTICE,
+				(errmsg("query contains only stopword(s) or doesn't contain lexeme(s), ignored")));
 		*len = 0;
 		return NULL;
 	}
diff --git a/src/backend/utils/adt/tsvector.c b/src/backend/utils/adt/tsvector.c
index fb44c1224f8..1d5e14feef7 100644
--- a/src/backend/utils/adt/tsvector.c
+++ b/src/backend/utils/adt/tsvector.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector.c,v 1.9 2007/11/16 15:05:59 teodor Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector.c,v 1.10 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -480,13 +480,13 @@ tsvectorrecv(PG_FUNCTION_ARGS)
 
 		lex_len = strlen(lexeme);
 		if (lex_len < 0 || lex_len > MAXSTRLEN)
-			elog(ERROR, "invalid tsvector; lexeme too long");
+			elog(ERROR, "invalid tsvector: lexeme too long");
 
 		if (datalen > MAXSTRPOS)
-			elog(ERROR, "invalid tsvector; maximum total lexeme length exceeded");
+			elog(ERROR, "invalid tsvector: maximum total lexeme length exceeded");
 
 		if (npos > MAXNUMPOS)
-			elog(ERROR, "unexpected number of positions");
+			elog(ERROR, "unexpected number of tsvector positions");
 
 		/*
 		 * Looks valid. Fill the WordEntry struct, and copy lexeme.
diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c
index 87694236238..6ca1c898332 100644
--- a/src/backend/utils/adt/tsvector_op.c
+++ b/src/backend/utils/adt/tsvector_op.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.9 2007/11/16 01:51:22 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_op.c,v 1.10 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -226,9 +226,9 @@ tsvector_setweight(PG_FUNCTION_ARGS)
 		case 'd':
 			w = 0;
 			break;
-			/* internal error */
 		default:
-			elog(ERROR, "unrecognized weight");
+			/* internal error */
+			elog(ERROR, "unrecognized weight: %d", cw);
 	}
 
 	out = (TSVector) palloc(VARSIZE(in));
@@ -609,7 +609,7 @@ TS_execute(QueryItem *curitem, void *checkval, bool calcnot,
 				return TS_execute(curitem + 1, checkval, calcnot, chkcond);
 
 		default:
-			elog(ERROR, "unknown operator %d", curitem->operator.oper);
+			elog(ERROR, "unrecognized operator: %d", curitem->operator.oper);
 	}
 
 	/* not reachable, but keep compiler quiet */
diff --git a/src/backend/utils/adt/tsvector_parser.c b/src/backend/utils/adt/tsvector_parser.c
index 5cb6a739b88..d0614eabd42 100644
--- a/src/backend/utils/adt/tsvector_parser.c
+++ b/src/backend/utils/adt/tsvector_parser.c
@@ -7,7 +7,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_parser.c,v 1.4 2007/11/15 22:25:16 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/tsvector_parser.c,v 1.5 2007/11/28 21:56:30 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -359,7 +359,8 @@ gettoken_tsvector(TSVectorParseState state,
 				PRSSYNTAXERROR;
 		}
 		else	/* internal error */
-			elog(ERROR, "internal error in gettoken_tsvector");
+			elog(ERROR, "unrecognized state in gettoken_tsvector: %d",
+				 statecode);
 
 		/* get next char */
 		state->prsbuf += pg_mblen(state->prsbuf);
-- 
GitLab