From 8a1684d9dc9cb5322d0e11244062ea79b8b53291 Mon Sep 17 00:00:00 2001
From: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Date: Fri, 2 Feb 2024 09:01:52 +0530
Subject: [PATCH v1 3/4] Unify error messages for various jsonpath item
 methods.

For numeric and string inputs, the error message string was different.
Also, for some inputs, the error message slightly seemed odd, like,
in the .boolean() case, we were saying "out of range" when the input
is 1.23. So, try to unify all those messages like the following
construct:

argument of jsonpath item method <name> is invalid for type <typename>

This also fixes the cases where we were saying "decimal or number"
that now say "type numeric" instead.

Jeevan Chalke, per discussion with Kyotaro Horiguchi and Tom Lane.

Discussion: https://www.postgresql.org/message-id/flat/CAM2+6=XjTyqrrqHAOj80r0wVQxJSxc0iyib9bPC55uFO9VKatg@mail.gmail.com
---
 src/backend/utils/adt/jsonpath_exec.c        | 24 +++++-----
 src/test/regress/expected/jsonb_jsonpath.out | 66 ++++++++++++++--------------
 2 files changed, 45 insertions(+), 45 deletions(-)

diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c
index 0d1d1b1..8bb1017 100644
--- a/src/backend/utils/adt/jsonpath_exec.c
+++ b/src/backend/utils/adt/jsonpath_exec.c
@@ -1052,7 +1052,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type double precision",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type double precision",
 													 jspOperationName(jsp->type)))));
 					if (isinf(val) || isnan(val))
 						RETURN_ERROR(ereport(ERROR,
@@ -1078,7 +1078,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of a double precision number",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type double precision",
 													 jspOperationName(jsp->type)))));
 					if (isinf(val) || isnan(val))
 						RETURN_ERROR(ereport(ERROR,
@@ -1166,7 +1166,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type bigint",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type bigint",
 													 jspOperationName(jsp->type)))));
 
 					datum = Int64GetDatum(val);
@@ -1188,7 +1188,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!noerr || escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of a big integer",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type bigint",
 													 jspOperationName(jsp->type)))));
 					res = jperOk;
 				}
@@ -1240,7 +1240,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!noerr || escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type boolean",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type boolean",
 													 jspOperationName(jsp->type)))));
 
 					ival = DatumGetInt32(datum);
@@ -1260,7 +1260,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!parse_bool(tmp, &bval))
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of a boolean",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type boolean",
 													 jspOperationName(jsp->type)))));
 
 					res = jperOk;
@@ -1322,7 +1322,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!noerr || escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of a decimal or number",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type numeric",
 													 jspOperationName(jsp->type)))));
 
 					num = DatumGetNumeric(datum);
@@ -1411,7 +1411,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!noerr || escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of a decimal or number",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type numeric",
 													 jspOperationName(jsp->type)))));
 
 					num = DatumGetNumeric(numdatum);
@@ -1444,7 +1444,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (have_error)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type integer",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type integer",
 													 jspOperationName(jsp->type)))));
 
 					datum = Int32GetDatum(val);
@@ -1466,7 +1466,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (!noerr || escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM),
-											  errmsg("string argument of jsonpath item method .%s() is not a valid representation of an integer",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type integer",
 													 jspOperationName(jsp->type)))));
 					res = jperOk;
 				}
@@ -2563,7 +2563,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type integer",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type integer",
 													 jspOperationName(jsp->type)))));
 					value = TimestampGetDatum(result);
 
@@ -2615,7 +2615,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
 					if (escontext.error_occurred)
 						RETURN_ERROR(ereport(ERROR,
 											 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_SQL_JSON_DATETIME_FUNCTION),
-											  errmsg("numeric argument of jsonpath item method .%s() is out of range for type integer",
+											  errmsg("argument of jsonpath item method .%s() is invalid for type integer",
 													 jspOperationName(jsp->type)))));
 					value = TimestampTzGetDatum(result);
 
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index 95408ad..5fc9fea 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -1496,9 +1496,9 @@ select jsonb_path_query('"1.23"', '$.double()');
 (1 row)
 
 select jsonb_path_query('"1.23aaa"', '$.double()');
-ERROR:  string argument of jsonpath item method .double() is not a valid representation of a double precision number
+ERROR:  argument of jsonpath item method .double() is invalid for type double precision
 select jsonb_path_query('1e1000', '$.double()');
-ERROR:  numeric argument of jsonpath item method .double() is out of range for type double precision
+ERROR:  argument of jsonpath item method .double() is invalid for type double precision
 select jsonb_path_query('"nan"', '$.double()');
 ERROR:  NaN or Infinity is not allowed for jsonpath item method .double()
 select jsonb_path_query('"NaN"', '$.double()');
@@ -1767,19 +1767,19 @@ select jsonb_path_query('{}', '$.bigint()', silent => true);
 (0 rows)
 
 select jsonb_path_query('"1.23"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"1.23aaa"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('1e1000', '$.bigint()');
-ERROR:  numeric argument of jsonpath item method .bigint() is out of range for type bigint
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"nan"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"NaN"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"inf"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"-inf"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"inf"', '$.bigint()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1827,9 +1827,9 @@ select jsonb_path_query('"1234567890123"', '$.bigint()');
 (1 row)
 
 select jsonb_path_query('12345678901234567890', '$.bigint()');
-ERROR:  numeric argument of jsonpath item method .bigint() is out of range for type bigint
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"12345678901234567890"', '$.bigint()');
-ERROR:  string argument of jsonpath item method .bigint() is not a valid representation of a big integer
+ERROR:  argument of jsonpath item method .bigint() is invalid for type bigint
 select jsonb_path_query('"+123"', '$.bigint()');
  jsonb_path_query 
 ------------------
@@ -1882,21 +1882,21 @@ select jsonb_path_query('{}', '$.boolean()', silent => true);
 (0 rows)
 
 select jsonb_path_query('1.23', '$.boolean()');
-ERROR:  numeric argument of jsonpath item method .boolean() is out of range for type boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"1.23"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"1.23aaa"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('1e1000', '$.boolean()');
-ERROR:  numeric argument of jsonpath item method .boolean() is out of range for type boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"nan"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"NaN"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"inf"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"-inf"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('"inf"', '$.boolean()', silent => true);
  jsonb_path_query 
 ------------------
@@ -1908,7 +1908,7 @@ select jsonb_path_query('"-inf"', '$.boolean()', silent => true);
 (0 rows)
 
 select jsonb_path_query('"100"', '$.boolean()');
-ERROR:  string argument of jsonpath item method .boolean() is not a valid representation of a boolean
+ERROR:  argument of jsonpath item method .boolean() is invalid for type boolean
 select jsonb_path_query('true', '$.boolean()');
  jsonb_path_query 
 ------------------
@@ -2144,7 +2144,7 @@ select jsonb_path_query('"1.23"', '$.decimal()');
 (1 row)
 
 select jsonb_path_query('"1.23aaa"', '$.decimal()');
-ERROR:  string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR:  argument of jsonpath item method .decimal() is invalid for type numeric
 select jsonb_path_query('1e1000', '$.decimal()');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              jsonb_path_query                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -2224,7 +2224,7 @@ select jsonb_path_query('12345.678', '$.decimal(6, 1)');
 (1 row)
 
 select jsonb_path_query('12345.678', '$.decimal(6, 2)');
-ERROR:  string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR:  argument of jsonpath item method .decimal() is invalid for type numeric
 select jsonb_path_query('1234.5678', '$.decimal(6, 2)');
  jsonb_path_query 
 ------------------
@@ -2232,7 +2232,7 @@ select jsonb_path_query('1234.5678', '$.decimal(6, 2)');
 (1 row)
 
 select jsonb_path_query('12345.678', '$.decimal(4, 6)');
-ERROR:  string argument of jsonpath item method .decimal() is not a valid representation of a decimal or number
+ERROR:  argument of jsonpath item method .decimal() is invalid for type numeric
 select jsonb_path_query('12345.678', '$.decimal(0, 6)');
 ERROR:  NUMERIC precision 0 must be between 1 and 1000
 select jsonb_path_query('12345.678', '$.decimal(1001, 6)');
@@ -2318,19 +2318,19 @@ select jsonb_path_query('{}', '$.integer()', silent => true);
 (0 rows)
 
 select jsonb_path_query('"1.23"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"1.23aaa"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('1e1000', '$.integer()');
-ERROR:  numeric argument of jsonpath item method .integer() is out of range for type integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"nan"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"NaN"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"inf"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"-inf"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"inf"', '$.integer()', silent => true);
  jsonb_path_query 
 ------------------
@@ -2366,9 +2366,9 @@ select jsonb_path_query('1.83', '$.integer()');
 (1 row)
 
 select jsonb_path_query('12345678901', '$.integer()');
-ERROR:  numeric argument of jsonpath item method .integer() is out of range for type integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"12345678901"', '$.integer()');
-ERROR:  string argument of jsonpath item method .integer() is not a valid representation of an integer
+ERROR:  argument of jsonpath item method .integer() is invalid for type integer
 select jsonb_path_query('"+123"', '$.integer()');
  jsonb_path_query 
 ------------------
@@ -2440,7 +2440,7 @@ select jsonb_path_query('"1.23"', '$.number()');
 (1 row)
 
 select jsonb_path_query('"1.23aaa"', '$.number()');
-ERROR:  string argument of jsonpath item method .number() is not a valid representation of a decimal or number
+ERROR:  argument of jsonpath item method .number() is invalid for type numeric
 select jsonb_path_query('1e1000', '$.number()');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              jsonb_path_query                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- 
1.8.3.1

