commit 36c3e9db5e63c8bd21973abaf43451fe10367e04
Author: Alexander Korotkov <akorotkov@postgresql.org>
Date:   Tue Apr 23 17:43:09 2019 +0300

    Better error reporting in jsonpath

diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 074cea24ae3..66557ff4f82 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -77,16 +77,6 @@
 #include "utils/varlena.h"
 
 
-/* Standard error message for SQL/JSON errors */
-#define ERRMSG_JSON_ARRAY_NOT_FOUND			"SQL/JSON array not found"
-#define ERRMSG_JSON_OBJECT_NOT_FOUND		"SQL/JSON object not found"
-#define ERRMSG_JSON_MEMBER_NOT_FOUND		"SQL/JSON member not found"
-#define ERRMSG_JSON_NUMBER_NOT_FOUND		"SQL/JSON number not found"
-#define ERRMSG_JSON_SCALAR_REQUIRED			"SQL/JSON scalar required"
-#define ERRMSG_SINGLETON_JSON_ITEM_REQUIRED	"singleton SQL/JSON item required"
-#define ERRMSG_NON_NUMERIC_JSON_ITEM		"non-numeric SQL/JSON item"
-#define ERRMSG_INVALID_JSON_SUBSCRIPT		"invalid SQL/JSON subscript"
-
 /*
  * Represents "base object" and it's "id" for .keyvalue() evaluation.
  */
@@ -349,8 +339,8 @@ jsonb_path_match(PG_FUNCTION_ARGS)
 	if (!silent)
 		ereport(ERROR,
 				(errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
-				 errmsg(ERRMSG_SINGLETON_JSON_ITEM_REQUIRED),
-				 errdetail("expression should return a singleton boolean")));
+				 errmsg("singleton SQL/JSON item required"),
+				 errdetail("Singleton boolean result is expected.")));
 
 	PG_RETURN_NULL();
 }
@@ -497,8 +487,8 @@ executeJsonPath(JsonPath *path, Jsonb *vars, Jsonb *json, bool throwErrors,
 	{
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("jsonb containing jsonpath variables "
-						"is not an object")));
+				 errmsg("\"vars\" argument is not an object"),
+				 errdetail("Jsonpath parameters should be encoded as key-value pairs of \"vars\" object.")));
 	}
 
 	cxt.vars = vars;
@@ -607,24 +597,17 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 				}
 				else if (!jspIgnoreStructuralErrors(cxt))
 				{
-					StringInfoData keybuf;
-					char	   *keystr;
-
 					Assert(found);
 
 					if (!jspThrowErrors(cxt))
 						return jperError;
 
-					initStringInfo(&keybuf);
-
-					keystr = pnstrdup(key.val.string.val, key.val.string.len);
-					escape_json(&keybuf, keystr);
-
 					ereport(ERROR,
 							(errcode(ERRCODE_JSON_MEMBER_NOT_FOUND), \
-							 errmsg(ERRMSG_JSON_MEMBER_NOT_FOUND),
-							 errdetail("JSON object does not contain key %s",
-									   keybuf.data)));
+							 errmsg("SQL/JSON member not found"),
+							 errdetail("JSON object does not contain key \"%s\".",
+									   pnstrdup(key.val.string.val,
+												key.val.string.len))));
 				}
 			}
 			else if (unwrap && JsonbType(jb) == jbvArray)
@@ -634,9 +617,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 				Assert(found);
 				RETURN_ERROR(ereport(ERROR,
 									 (errcode(ERRCODE_JSON_MEMBER_NOT_FOUND),
-									  errmsg(ERRMSG_JSON_MEMBER_NOT_FOUND),
-									  errdetail("jsonpath member accessor can "
-												"only be applied to an object"))));
+									  errmsg("SQL/JSON member not found"),
+									  errdetail("Jsonpath member accessor can only be applied to an object."))));
 			}
 			break;
 
@@ -665,9 +647,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 			else if (!jspIgnoreStructuralErrors(cxt))
 				RETURN_ERROR(ereport(ERROR,
 									 (errcode(ERRCODE_JSON_ARRAY_NOT_FOUND),
-									  errmsg(ERRMSG_JSON_ARRAY_NOT_FOUND),
-									  errdetail("jsonpath wildcard array accessor "
-												"can only be applied to an array"))));
+									  errmsg("SQL/JSON array not found"),
+									  errdetail("Jsonpath wildcard array accessor can only be applied to an array."))));
 			break;
 
 		case jpiIndexArray:
@@ -715,9 +696,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 						 index_to >= size))
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_INVALID_JSON_SUBSCRIPT),
-											  errmsg(ERRMSG_INVALID_JSON_SUBSCRIPT),
-											  errdetail("jsonpath array subscript is "
-														"out of bounds"))));
+											  errmsg("invalid SQL/JSON subscript"),
+											  errdetail("Jsonpath array subscript is out of bounds."))));
 
 					if (index_from < 0)
 						index_from = 0;
@@ -774,9 +754,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 			{
 				RETURN_ERROR(ereport(ERROR,
 									 (errcode(ERRCODE_JSON_ARRAY_NOT_FOUND),
-									  errmsg(ERRMSG_JSON_ARRAY_NOT_FOUND),
-									  errdetail("jsonpath array accessor can "
-												"only be applied to an array"))));
+									  errmsg("SQL/JSON array not found"),
+									  errdetail("Jsonpath array accessor can only be applied to an array."))));
 			}
 			break;
 
@@ -788,8 +767,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 				bool		hasNext = jspGetNext(jsp, &elem);
 
 				if (cxt->innermostArraySize < 0)
-					elog(ERROR, "evaluating jsonpath LAST outside of "
-						 "array subscript");
+					elog(ERROR, "evaluating jsonpath LAST outside of array subscript");
 
 				if (!hasNext && !found)
 				{
@@ -831,9 +809,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 				Assert(found);
 				RETURN_ERROR(ereport(ERROR,
 									 (errcode(ERRCODE_JSON_OBJECT_NOT_FOUND),
-									  errmsg(ERRMSG_JSON_OBJECT_NOT_FOUND),
-									  errdetail("jsonpath wildcard member accessor "
-												"can only be applied to an object"))));
+									  errmsg("SQL/JSON object not found"),
+									  errdetail("Jsonpath wildcard member accessor can only be applied to an object."))));
 			}
 			break;
 
@@ -963,9 +940,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 						if (!jspIgnoreStructuralErrors(cxt))
 							RETURN_ERROR(ereport(ERROR,
 												 (errcode(ERRCODE_JSON_ARRAY_NOT_FOUND),
-												  errmsg(ERRMSG_JSON_ARRAY_NOT_FOUND),
-												  errdetail("jsonpath item method .%s() "
-															"can only be applied to an array",
+												  errmsg("SQL/JSON array not found"),
+												  errdetail("Jsonpath item method .%s() can only be applied to an array.",
 															jspOperationName(jsp->type)))));
 						break;
 					}
@@ -1019,10 +995,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_JSON_ITEM),
-											  errmsg(ERRMSG_NON_NUMERIC_JSON_ITEM),
-											  errdetail("jsonpath item method .%s() "
-														"can only be applied to "
-														"a numeric value",
+											  errmsg("non-numeric SQL/JSON item"),
+											  errdetail("Jsonpath item method .%s() can only be applied to a numeric value.",
 														jspOperationName(jsp->type)))));
 					res = jperOk;
 				}
@@ -1043,9 +1017,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error || isinf(val))
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_JSON_ITEM),
-											  errmsg(ERRMSG_NON_NUMERIC_JSON_ITEM),
-											  errdetail("jsonpath item method .%s() can "
-														"only be applied to a numeric value",
+											  errmsg("non-numeric SQL/JSON item"),
+											  errdetail("Jsonpath item method .%s() can only be applied to a numeric value.",
 														jspOperationName(jsp->type)))));
 
 					jb = &jbv;
@@ -1058,10 +1031,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 				if (res == jperNotFound)
 					RETURN_ERROR(ereport(ERROR,
 										 (errcode(ERRCODE_NON_NUMERIC_JSON_ITEM),
-										  errmsg(ERRMSG_NON_NUMERIC_JSON_ITEM),
-										  errdetail("jsonpath item method .%s() "
-													"can only be applied to a "
-													"string or numeric value",
+										  errmsg("non-numeric SQL/JSON item"),
+										  errdetail("Jsonpath item method .%s() can only be applied to a string or numeric value.",
 													jspOperationName(jsp->type)))));
 
 				res = executeNextItem(cxt, jsp, NULL, jb, found, true);
@@ -1545,18 +1516,16 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
 		!(lval = getScalar(JsonValueListHead(&lseq), jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
-							  errmsg(ERRMSG_SINGLETON_JSON_ITEM_REQUIRED),
-							  errdetail("left operand of binary jsonpath operator %s "
-										"is not a singleton numeric value",
+							  errmsg("singleton SQL/JSON item required"),
+							  errdetail("Left operand of binary jsonpath operator %s is not a singleton numeric value.",
 										jspOperationName(jsp->type)))));
 
 	if (JsonValueListLength(&rseq) != 1 ||
 		!(rval = getScalar(JsonValueListHead(&rseq), jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_SINGLETON_JSON_ITEM_REQUIRED),
-							  errmsg(ERRMSG_SINGLETON_JSON_ITEM_REQUIRED),
-							  errdetail("right operand of binary jsonpath operator %s "
-										"is not a singleton numeric value",
+							  errmsg("singleton SQL/JSON item required"),
+							  errdetail("Right operand of binary jsonpath operator %s is not a singleton numeric value.",
 										jspOperationName(jsp->type)))));
 
 	if (jspThrowErrors(cxt))
@@ -1624,9 +1593,8 @@ executeUnaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
 
 			RETURN_ERROR(ereport(ERROR,
 								 (errcode(ERRCODE_JSON_NUMBER_NOT_FOUND),
-								  errmsg(ERRMSG_JSON_NUMBER_NOT_FOUND),
-								  errdetail("operand of unary jsonpath operator %s "
-											"is not a numeric value",
+								  errmsg("SQL/JSON number not found"),
+								  errdetail("Operand of unary jsonpath operator %s is not a numeric value.",
 											jspOperationName(jsp->type)))));
 		}
 
@@ -1737,9 +1705,8 @@ executeNumericItemMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
 	if (!(jb = getScalar(jb, jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_NON_NUMERIC_JSON_ITEM),
-							  errmsg(ERRMSG_NON_NUMERIC_JSON_ITEM),
-							  errdetail("jsonpath item method .%s() can only "
-										"be applied to a numeric value",
+							  errmsg("non-numeric SQL/JSON item"),
+							  errdetail("Jsonpath item method .%s() can only be applied to a numeric value.",
 										jspOperationName(jsp->type)))));
 
 	datum = NumericGetDatum(jb->val.numeric);
@@ -1799,9 +1766,8 @@ executeKeyValueMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
 	if (JsonbType(jb) != jbvObject || jb->type != jbvBinary)
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_JSON_OBJECT_NOT_FOUND),
-							  errmsg(ERRMSG_JSON_OBJECT_NOT_FOUND),
-							  errdetail("jsonpath item method .%s() "
-										"can only be applied to an object",
+							  errmsg("SQL/JSON object not found"),
+							  errdetail("Jsonpath item method .%s() can only be applied to an object.",
 										jspOperationName(jsp->type)))));
 
 	jbc = jb->val.binary.data;
@@ -1984,7 +1950,7 @@ getJsonPathVariable(JsonPathExecContext *cxt, JsonPathItem *variable,
 	{
 		ereport(ERROR,
 				(errcode(ERRCODE_UNDEFINED_OBJECT),
-				 errmsg("cannot find jsonpath variable '%s'",
+				 errmsg("cannot find jsonpath variable \"%s\"",
 						pnstrdup(varName, varNameLength))));
 	}
 
@@ -2144,9 +2110,8 @@ getArrayIndex(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
 		!(jbv = getScalar(JsonValueListHead(&found), jbvNumeric)))
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_INVALID_JSON_SUBSCRIPT),
-							  errmsg(ERRMSG_INVALID_JSON_SUBSCRIPT),
-							  errdetail("jsonpath array subscript is not a "
-										"singleton numeric value"))));
+							  errmsg("invalid SQL/JSON subscript"),
+							  errdetail("Jsonpath array subscript is not a singleton numeric value."))));
 
 	numeric_index = DirectFunctionCall2(numeric_trunc,
 										NumericGetDatum(jbv->val.numeric),
@@ -2158,9 +2123,8 @@ getArrayIndex(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue *jb,
 	if (have_error)
 		RETURN_ERROR(ereport(ERROR,
 							 (errcode(ERRCODE_INVALID_JSON_SUBSCRIPT),
-							  errmsg(ERRMSG_INVALID_JSON_SUBSCRIPT),
-							  errdetail("jsonpath array subscript is "
-										"out of integer range"))));
+							  errmsg("invalid SQL/JSON subscript"),
+							  errdetail("Jsonpath array subscript is out of integer range."))));
 
 	return jperOk;
 }
diff --git a/src/backend/utils/adt/jsonpath_gram.y b/src/backend/utils/adt/jsonpath_gram.y
index 76155963fc6..d9d54b35cd5 100644
--- a/src/backend/utils/adt/jsonpath_gram.y
+++ b/src/backend/utils/adt/jsonpath_gram.y
@@ -509,7 +509,11 @@ makeItemLikeRegex(JsonPathParseItem *expr, JsonPathString *pattern,
 				cflags |= REG_EXPANDED;
 				break;
 			default:
-				yyerror(NULL, "unrecognized flag of LIKE_REGEX predicate");
+				ereport(ERROR,
+						(errcode(ERRCODE_SYNTAX_ERROR),
+						 errmsg("invalid input syntax for type %s", "jsonpath"),
+						 errdetail("unrecognized flag character \"%c\" in LIKE_REGEX predicate",
+								   flags->val[i])));
 				break;
 		}
 	}
diff --git a/src/backend/utils/adt/jsonpath_scan.l b/src/backend/utils/adt/jsonpath_scan.l
index 72bb5e3937b..651fec455ba 100644
--- a/src/backend/utils/adt/jsonpath_scan.l
+++ b/src/backend/utils/adt/jsonpath_scan.l
@@ -295,7 +295,7 @@ jsonpath_yyerror(JsonPathParseResult **result, const char *message)
 	{
 		ereport(ERROR,
 				(errcode(ERRCODE_SYNTAX_ERROR),
-				 errmsg("bad jsonpath representation"),
+				 errmsg("invalid input syntax for type %s", "jsonpath"),
 				 /* translator: %s is typically "syntax error" */
 				 errdetail("%s at end of input", message)));
 	}
@@ -303,7 +303,7 @@ jsonpath_yyerror(JsonPathParseResult **result, const char *message)
 	{
 		ereport(ERROR,
 				(errcode(ERRCODE_SYNTAX_ERROR),
-				 errmsg("bad jsonpath representation"),
+				 errmsg("invalid input syntax for type %s", "jsonpath"),
 				 /* translator: first %s is typically "syntax error" */
 				 errdetail("%s at or near \"%s\"", message, yytext)));
 	}
@@ -495,7 +495,7 @@ hexval(char c)
 		return c - 'a' + 0xA;
 	if (c >= 'A' && c <= 'F')
 		return c - 'A' + 0xA;
-	elog(ERROR, "invalid hexadecimal digit");
+	jsonpath_yyerror(NULL, "invalid hexadecimal digit");
 	return 0; /* not reached */
 }
 
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index 49a857bca62..d637c3b4c44 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -120,7 +120,7 @@ select jsonb '[1]' @? 'strict $[1]';
 
 select jsonb_path_query('[1]', 'strict $[1]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is out of bounds
+DETAIL:  Jsonpath array subscript is out of bounds.
 select jsonb_path_query('[1]', 'strict $[1]', silent => true);
  jsonb_path_query 
 ------------------
@@ -140,10 +140,10 @@ select jsonb '[1]' @? 'strict $[10000000000000000]';
 
 select jsonb_path_query('[1]', 'lax $[10000000000000000]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is out of integer range
+DETAIL:  Jsonpath array subscript is out of integer range.
 select jsonb_path_query('[1]', 'strict $[10000000000000000]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is out of integer range
+DETAIL:  Jsonpath array subscript is out of integer range.
 select jsonb '[1]' @? '$[0]';
  ?column? 
 ----------
@@ -242,7 +242,7 @@ select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'lax $[*].a', silent => true
 
 select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'strict $[*].a', silent => false);
 ERROR:  SQL/JSON member not found
-DETAIL:  jsonpath member accessor can only be applied to an object
+DETAIL:  Jsonpath member accessor can only be applied to an object.
 select jsonb_path_exists('[{"a": 1}, {"a": 2}, 3]', 'strict $[*].a', silent => true);
  jsonb_path_exists 
 -------------------
@@ -256,10 +256,10 @@ select jsonb_path_query('1', 'lax $.a');
 
 select jsonb_path_query('1', 'strict $.a');
 ERROR:  SQL/JSON member not found
-DETAIL:  jsonpath member accessor can only be applied to an object
+DETAIL:  Jsonpath member accessor can only be applied to an object.
 select jsonb_path_query('1', 'strict $.*');
 ERROR:  SQL/JSON object not found
-DETAIL:  jsonpath wildcard member accessor can only be applied to an object
+DETAIL:  Jsonpath wildcard member accessor can only be applied to an object.
 select jsonb_path_query('1', 'strict $.a', silent => true);
  jsonb_path_query 
 ------------------
@@ -277,7 +277,7 @@ select jsonb_path_query('[]', 'lax $.a');
 
 select jsonb_path_query('[]', 'strict $.a');
 ERROR:  SQL/JSON member not found
-DETAIL:  jsonpath member accessor can only be applied to an object
+DETAIL:  Jsonpath member accessor can only be applied to an object.
 select jsonb_path_query('[]', 'strict $.a', silent => true);
  jsonb_path_query 
 ------------------
@@ -290,7 +290,7 @@ select jsonb_path_query('{}', 'lax $.a');
 
 select jsonb_path_query('{}', 'strict $.a');
 ERROR:  SQL/JSON member not found
-DETAIL:  JSON object does not contain key "a"
+DETAIL:  JSON object does not contain key "a".
 select jsonb_path_query('{}', 'strict $.a', silent => true);
  jsonb_path_query 
 ------------------
@@ -298,16 +298,16 @@ select jsonb_path_query('{}', 'strict $.a', silent => true);
 
 select jsonb_path_query('1', 'strict $[1]');
 ERROR:  SQL/JSON array not found
-DETAIL:  jsonpath array accessor can only be applied to an array
+DETAIL:  Jsonpath array accessor can only be applied to an array.
 select jsonb_path_query('1', 'strict $[*]');
 ERROR:  SQL/JSON array not found
-DETAIL:  jsonpath wildcard array accessor can only be applied to an array
+DETAIL:  Jsonpath wildcard array accessor can only be applied to an array.
 select jsonb_path_query('[]', 'strict $[1]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is out of bounds
+DETAIL:  Jsonpath array subscript is out of bounds.
 select jsonb_path_query('[]', 'strict $["a"]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is not a singleton numeric value
+DETAIL:  Jsonpath array subscript is not a singleton numeric value.
 select jsonb_path_query('1', 'strict $[1]', silent => true);
  jsonb_path_query 
 ------------------
@@ -438,7 +438,7 @@ select jsonb_path_query('[1,2,3]', 'lax $[*]');
 
 select jsonb_path_query('[1,2,3]', 'strict $[*].a');
 ERROR:  SQL/JSON member not found
-DETAIL:  jsonpath member accessor can only be applied to an object
+DETAIL:  Jsonpath member accessor can only be applied to an object.
 select jsonb_path_query('[1,2,3]', 'strict $[*].a', silent => true);
  jsonb_path_query 
 ------------------
@@ -456,7 +456,7 @@ select jsonb_path_query('[]', '$[last ? (exists(last))]');
 
 select jsonb_path_query('[]', 'strict $[last]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is out of bounds
+DETAIL:  Jsonpath array subscript is out of bounds.
 select jsonb_path_query('[]', 'strict $[last]', silent => true);
  jsonb_path_query 
 ------------------
@@ -488,7 +488,7 @@ select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "number")]');
 
 select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]');
 ERROR:  invalid SQL/JSON subscript
-DETAIL:  jsonpath array subscript is not a singleton numeric value
+DETAIL:  Jsonpath array subscript is not a singleton numeric value.
 select jsonb_path_query('[1,2,3]', '$[last ? (@.type() == "string")]', silent => true);
  jsonb_path_query 
 ------------------
@@ -501,11 +501,13 @@ select * from jsonb_path_query('{"a": 10}', '$');
 (1 row)
 
 select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)');
-ERROR:  cannot find jsonpath variable 'value'
+ERROR:  cannot find jsonpath variable "value"
 select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '1');
-ERROR:  jsonb containing jsonpath variables is not an object
+ERROR:  "vars" argument is not an object
+DETAIL:  Jsonpath parameters should be encoded as key-value pairs of "vars" object.
 select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '[{"value" : 13}]');
-ERROR:  jsonb containing jsonpath variables is not an object
+ERROR:  "vars" argument is not an object
+DETAIL:  Jsonpath parameters should be encoded as key-value pairs of "vars" object.
 select * from jsonb_path_query('{"a": 10}', '$ ? (@.a < $value)', '{"value" : 13}');
  jsonb_path_query 
 ------------------
@@ -1068,16 +1070,16 @@ select jsonb_path_query('0', '-(3 + 1 % $)');
 ERROR:  division by zero
 select jsonb_path_query('1', '$ + "2"');
 ERROR:  singleton SQL/JSON item required
-DETAIL:  right operand of binary jsonpath operator + is not a singleton numeric value
+DETAIL:  Right operand of binary jsonpath operator + is not a singleton numeric value.
 select jsonb_path_query('[1, 2]', '3 * $');
 ERROR:  singleton SQL/JSON item required
-DETAIL:  right operand of binary jsonpath operator * is not a singleton numeric value
+DETAIL:  Right operand of binary jsonpath operator * is not a singleton numeric value.
 select jsonb_path_query('"a"', '-$');
 ERROR:  SQL/JSON number not found
-DETAIL:  operand of unary jsonpath operator - is not a numeric value
+DETAIL:  Operand of unary jsonpath operator - is not a numeric value.
 select jsonb_path_query('[1,"2",3]', '+$');
 ERROR:  SQL/JSON number not found
-DETAIL:  operand of unary jsonpath operator + is not a numeric value
+DETAIL:  Operand of unary jsonpath operator + is not a numeric value.
 select jsonb_path_query('1', '$ + "2"', silent => true);
  jsonb_path_query 
 ------------------
@@ -1147,7 +1149,7 @@ select jsonb_path_query('{"a": [2, 3, 4]}', 'lax -$.a');
 -- should fail
 select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3');
 ERROR:  singleton SQL/JSON item required
-DETAIL:  left operand of binary jsonpath operator * is not a singleton numeric value
+DETAIL:  Left operand of binary jsonpath operator * is not a singleton numeric value.
 select jsonb_path_query('{"a": [1, 2]}', 'lax $.a * 3', silent => true);
  jsonb_path_query 
 ------------------
@@ -1347,7 +1349,7 @@ select jsonb_path_query('[1, 2, 3]', 'strict ($[*].a > 3).type()');
 
 select jsonb_path_query('[1,null,true,"11",[],[1],[1,2,3],{},{"a":1,"b":2}]', 'strict $[*].size()');
 ERROR:  SQL/JSON array not found
-DETAIL:  jsonpath item method .size() can only be applied to an array
+DETAIL:  Jsonpath item method .size() can only be applied to an array.
 select jsonb_path_query('[1,null,true,"11",[],[1],[1,2,3],{},{"a":1,"b":2}]', 'strict $[*].size()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1419,7 +1421,7 @@ select jsonb_path_query('[0, 1, -2, -3.4, 5.6]', '$[*].ceiling().abs().type()');
 
 select jsonb_path_query('[{},1]', '$[*].keyvalue()');
 ERROR:  SQL/JSON object not found
-DETAIL:  jsonpath item method .keyvalue() can only be applied to an object
+DETAIL:  Jsonpath item method .keyvalue() can only be applied to an object.
 select jsonb_path_query('[{},1]', '$[*].keyvalue()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1448,7 +1450,7 @@ select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', '$[*].ke
 
 select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'strict $.keyvalue()');
 ERROR:  SQL/JSON object not found
-DETAIL:  jsonpath item method .keyvalue() can only be applied to an object
+DETAIL:  Jsonpath item method .keyvalue() can only be applied to an object.
 select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'lax $.keyvalue()');
                jsonb_path_query                
 -----------------------------------------------
@@ -1459,7 +1461,7 @@ select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'lax $.k
 
 select jsonb_path_query('[{"a": 1, "b": [1, 2]}, {"c": {"a": "bbb"}}]', 'strict $.keyvalue().a');
 ERROR:  SQL/JSON object not found
-DETAIL:  jsonpath item method .keyvalue() can only be applied to an object
+DETAIL:  Jsonpath item method .keyvalue() can only be applied to an object.
 select jsonb '{"a": 1, "b": [1, 2]}' @? 'lax $.keyvalue()';
  ?column? 
 ----------
@@ -1474,10 +1476,10 @@ select jsonb '{"a": 1, "b": [1, 2]}' @? 'lax $.keyvalue().key';
 
 select jsonb_path_query('null', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a string or numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a string or numeric value.
 select jsonb_path_query('true', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a string or numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a string or numeric value.
 select jsonb_path_query('null', '$.double()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1495,10 +1497,10 @@ select jsonb_path_query('[]', '$.double()');
 
 select jsonb_path_query('[]', 'strict $.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a string or numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a string or numeric value.
 select jsonb_path_query('{}', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a string or numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a string or numeric value.
 select jsonb_path_query('[]', 'strict $.double()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1523,7 +1525,7 @@ select jsonb_path_query('"1.23"', '$.double()');
 
 select jsonb_path_query('"1.23aaa"', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a numeric value.
 select jsonb_path_query('"nan"', '$.double()');
  jsonb_path_query 
 ------------------
@@ -1538,10 +1540,10 @@ select jsonb_path_query('"NaN"', '$.double()');
 
 select jsonb_path_query('"inf"', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a numeric value.
 select jsonb_path_query('"-inf"', '$.double()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .double() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .double() can only be applied to a numeric value.
 select jsonb_path_query('"inf"', '$.double()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1554,13 +1556,13 @@ select jsonb_path_query('"-inf"', '$.double()', silent => true);
 
 select jsonb_path_query('{}', '$.abs()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .abs() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .abs() can only be applied to a numeric value.
 select jsonb_path_query('true', '$.floor()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .floor() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .floor() can only be applied to a numeric value.
 select jsonb_path_query('"1.2"', '$.ceiling()');
 ERROR:  non-numeric SQL/JSON item
-DETAIL:  jsonpath item method .ceiling() can only be applied to a numeric value
+DETAIL:  Jsonpath item method .ceiling() can only be applied to a numeric value.
 select jsonb_path_query('{}', '$.abs()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1669,7 +1671,7 @@ SELECT jsonb_path_query('[{"a": 1}, {"a": 2}]', '$[*] ? (@.a > 10)');
 
 SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
 ERROR:  SQL/JSON member not found
-DETAIL:  JSON object does not contain key "a"
+DETAIL:  JSON object does not contain key "a".
 SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}]', '$[*].a');
  jsonb_path_query_array 
 ------------------------
@@ -1702,7 +1704,7 @@ SELECT jsonb_path_query_array('[{"a": 1}, {"a": 2}, {"a": 3}, {"a": 5}]', '$[*].
 
 SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a');
 ERROR:  SQL/JSON member not found
-DETAIL:  JSON object does not contain key "a"
+DETAIL:  JSON object does not contain key "a".
 SELECT jsonb_path_query_first('[{"a": 1}, {"a": 2}, {}]', 'strict $[*].a', silent => true);
  jsonb_path_query_first 
 ------------------------
@@ -1795,22 +1797,22 @@ SELECT jsonb_path_match('1', '$', silent => true);
 
 SELECT jsonb_path_match('1', '$', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb_path_match('"a"', '$', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb_path_match('{}', '$', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb_path_match('[true]', '$', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb_path_match('{}', 'lax $.a', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb_path_match('{}', 'strict $.a', silent => false);
 ERROR:  SQL/JSON member not found
-DETAIL:  JSON object does not contain key "a"
+DETAIL:  JSON object does not contain key "a".
 SELECT jsonb_path_match('{}', 'strict $.a', silent => true);
  jsonb_path_match 
 ------------------
@@ -1819,7 +1821,7 @@ SELECT jsonb_path_match('{}', 'strict $.a', silent => true);
 
 SELECT jsonb_path_match('[true, true]', '$[*]', silent => false);
 ERROR:  singleton SQL/JSON item required
-DETAIL:  expression should return a singleton boolean
+DETAIL:  Singleton boolean result is expected.
 SELECT jsonb '[{"a": 1}, {"a": 2}]' @@ '$[*].a > 1';
  ?column? 
 ----------
diff --git a/src/test/regress/expected/jsonpath.out b/src/test/regress/expected/jsonpath.out
index a99643f9027..2ea9d89aa97 100644
--- a/src/test/regress/expected/jsonpath.out
+++ b/src/test/regress/expected/jsonpath.out
@@ -454,10 +454,10 @@ select '$ ? (@ like_regex "pattern" flag "xsms")'::jsonpath;
 (1 row)
 
 select '$ ? (@ like_regex "pattern" flag "a")'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@ like_regex "pattern" flag "a")'::jsonpath;
                ^
-DETAIL:  unrecognized flag of LIKE_REGEX predicate at or near """
+DETAIL:  unrecognized flag character "a" in LIKE_REGEX predicate
 select '$ < 1'::jsonpath;
  jsonpath 
 ----------
@@ -547,17 +547,17 @@ select '$ ? (@.a < +1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < .1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < -.1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < -.1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < +.1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < +.1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
@@ -616,17 +616,17 @@ select '$ ? (@.a < +1e1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < .1e1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < -.1e1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < -.1e1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < +.1e1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < +.1e1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
@@ -685,17 +685,17 @@ select '$ ? (@.a < +1e-1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e-1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < .1e-1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < -.1e-1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < -.1e-1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < +.1e-1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < +.1e-1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
@@ -754,17 +754,17 @@ select '$ ? (@.a < +1e+1)'::jsonpath;
 (1 row)
 
 select '$ ? (@.a < .1e+1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < .1e+1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < -.1e+1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < -.1e+1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '$ ? (@.a < +.1e+1)'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '$ ? (@.a < +.1e+1)'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
@@ -811,7 +811,7 @@ select '0'::jsonpath;
 (1 row)
 
 select '00'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '00'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected IDENT_P at end of input
@@ -870,7 +870,7 @@ select '0.0010e+2'::jsonpath;
 (1 row)
 
 select '1e'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '1e'::jsonpath;
                ^
 DETAIL:  Floating point number is invalid at or near "1e"
@@ -881,7 +881,7 @@ select '1.e'::jsonpath;
 (1 row)
 
 select '1.2e'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '1.2e'::jsonpath;
                ^
 DETAIL:  Floating point number is invalid at or near "1.2e"
@@ -940,22 +940,22 @@ select '(1.2).e3'::jsonpath;
 (1 row)
 
 select '1..e'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '1..e'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '1..e3'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '1..e3'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected '.' at or near "."
 select '(1.).e'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '(1.).e'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected ')' at or near ")"
 select '(1.).e3'::jsonpath;
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: select '(1.).e3'::jsonpath;
                ^
 DETAIL:  syntax error, unexpected ')' at or near ")"
diff --git a/src/test/regress/expected/jsonpath_encoding.out b/src/test/regress/expected/jsonpath_encoding.out
index 6d828d17248..b16f76483ac 100644
--- a/src/test/regress/expected/jsonpath_encoding.out
+++ b/src/test/regress/expected/jsonpath_encoding.out
@@ -2,17 +2,17 @@
 -- checks for double-quoted values
 -- basic unicode input
 SELECT '"\u"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u"
 SELECT '"\u00"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u00"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u00"
 SELECT '"\u000g"'::jsonpath;	-- ERROR, g is not a hex digit
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u000g"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u000"
@@ -165,17 +165,17 @@ DETAIL:  \u0000 cannot be converted to text.
 -- checks for quoted key names
 -- basic unicode input
 SELECT '$."\u"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u"
 SELECT '$."\u00"'::jsonpath;	-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u00"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u00"
 SELECT '$."\u000g"'::jsonpath;	-- ERROR, g is not a hex digit
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u000g"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u000"
diff --git a/src/test/regress/expected/jsonpath_encoding_1.out b/src/test/regress/expected/jsonpath_encoding_1.out
index 04179a8df79..a3a44e182ab 100644
--- a/src/test/regress/expected/jsonpath_encoding_1.out
+++ b/src/test/regress/expected/jsonpath_encoding_1.out
@@ -2,17 +2,17 @@
 -- checks for double-quoted values
 -- basic unicode input
 SELECT '"\u"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u"
 SELECT '"\u00"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u00"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u00"
 SELECT '"\u000g"'::jsonpath;	-- ERROR, g is not a hex digit
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '"\u000g"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u000"
@@ -156,17 +156,17 @@ DETAIL:  \u0000 cannot be converted to text.
 -- checks for quoted key names
 -- basic unicode input
 SELECT '$."\u"'::jsonpath;		-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u"
 SELECT '$."\u00"'::jsonpath;	-- ERROR, incomplete escape
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u00"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u00"
 SELECT '$."\u000g"'::jsonpath;	-- ERROR, g is not a hex digit
-ERROR:  bad jsonpath representation
+ERROR:  invalid input syntax for type jsonpath
 LINE 1: SELECT '$."\u000g"'::jsonpath;
                ^
 DETAIL:  Unicode sequence is invalid at or near "\u000"
