From 930e8ad738f5c78abd667626ca86f2553f36356f Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Tue, 5 Aug 2014 12:26:25 -0400
Subject: [PATCH] Improve some JSON error messages.

These messages are new in 9.4, which hasn't been released yet, so
back-patch to REL9_4_STABLE.

Daniele Varrazzo
---
 src/backend/utils/adt/json.c       | 19 ++++++++++++-------
 src/test/regress/expected/json.out |  3 ++-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 8dd7a21fcf..5bd39f5015 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -1910,7 +1910,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
 	if (val_type == InvalidOid || val_type == UNKNOWNOID)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("arg 1: could not determine data type")));
+				 errmsg("could not determine data type for argument %d", 1)));
 
 	add_json(arg, false, state, val_type, true);
 
@@ -1934,7 +1934,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
 	if (val_type == InvalidOid || val_type == UNKNOWNOID)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("arg 2: could not determine data type")));
+				 errmsg("could not determine data type for argument %d", 2)));
 
 	add_json(arg, PG_ARGISNULL(2), state, val_type, false);
 
@@ -1980,7 +1980,8 @@ json_build_object(PG_FUNCTION_ARGS)
 	if (nargs % 2 != 0)
 		ereport(ERROR,
 				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("invalid number or arguments: object must be matched key value pairs")));
+				 errmsg("invalid number or arguments"),
+				 errhint("Object must be matched key value pairs.")));
 
 	result = makeStringInfo();
 
@@ -1994,7 +1995,8 @@ json_build_object(PG_FUNCTION_ARGS)
 		if (PG_ARGISNULL(i))
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("arg %d: key cannot be null", i + 1)));
+					 errmsg("argument %d cannot be null", i + 1),
+					 errhint("Object keys should be text.")));
 		val_type = get_fn_expr_argtype(fcinfo->flinfo, i);
 
 		/*
@@ -2016,7 +2018,8 @@ json_build_object(PG_FUNCTION_ARGS)
 		if (val_type == InvalidOid || val_type == UNKNOWNOID)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("arg %d: could not determine data type", i + 1)));
+					 errmsg("could not determine data type for argument %d",
+							i + 1)));
 		appendStringInfoString(result, sep);
 		sep = ", ";
 		add_json(arg, false, result, val_type, true);
@@ -2042,7 +2045,8 @@ json_build_object(PG_FUNCTION_ARGS)
 		if (val_type == InvalidOid || val_type == UNKNOWNOID)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("arg %d: could not determine data type", i + 2)));
+					 errmsg("could not determine data type for argument %d",
+							i + 2)));
 		add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
 
 	}
@@ -2099,7 +2103,8 @@ json_build_array(PG_FUNCTION_ARGS)
 		if (val_type == InvalidOid || val_type == UNKNOWNOID)
 			ereport(ERROR,
 					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-					 errmsg("arg %d: could not determine data type", i + 1)));
+					 errmsg("could not determine data type for argument %d",
+							i + 1)));
 		appendStringInfoString(result, sep);
 		sep = ", ";
 		add_json(arg, PG_ARGISNULL(i), result, val_type, false);
diff --git a/src/test/regress/expected/json.out b/src/test/regress/expected/json.out
index de4d49d5b2..511b6e1513 100644
--- a/src/test/regress/expected/json.out
+++ b/src/test/regress/expected/json.out
@@ -1150,7 +1150,8 @@ SELECT json_build_object(1,2);
 
 -- keys must be scalar and not null
 SELECT json_build_object(null,2);
-ERROR:  arg 1: key cannot be null
+ERROR:  argument 1 cannot be null
+HINT:  Object keys should be text.
 SELECT json_build_object(r,2) FROM (SELECT 1 AS a, 2 AS b) r;
 ERROR:  key value must be scalar, not array, composite, or json
 SELECT json_build_object(json '{"a":1,"b":2}', 3);
-- 
2.49.0