Refactor/reword some error messages to avoid duplicates
authorAlvaro Herrera <[email protected]>
Wed, 7 Aug 2024 15:30:36 +0000 (11:30 -0400)
committerAlvaro Herrera <[email protected]>
Wed, 7 Aug 2024 15:30:36 +0000 (11:30 -0400)
Also, remove brackets around "EMPTY [ ARRAY ]".  An error message is
not the place to state that a keyword is optional.

Backpatch to 17.

src/backend/executor/execExprInterp.c
src/backend/parser/parse_expr.c
src/backend/parser/parse_jsontable.c
src/test/regress/expected/sqljson_jsontable.out
src/test/regress/expected/sqljson_queryfuncs.out

index 1535fd6b984519945ff26d3f532d9aca1f650227..ea47c4d6f9cfeb74f548b4c1122e0061e2c34db4 100644 (file)
@@ -4660,13 +4660,17 @@ ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op)
        if (DatumGetBool(jsestate->error.value))
            ereport(ERROR,
                    (errcode(ERRCODE_DATATYPE_MISMATCH),
-                    errmsg("could not coerce ON ERROR expression (%s) to the RETURNING type",
+           /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
+                    errmsg("could not coerce %s expression (%s) to the RETURNING type",
+                           "ON ERROR",
                            GetJsonBehaviorValueString(jsestate->jsexpr->on_error)),
                     errdetail("%s", jsestate->escontext.error_data->message)));
        else if (DatumGetBool(jsestate->empty.value))
            ereport(ERROR,
                    (errcode(ERRCODE_DATATYPE_MISMATCH),
-                    errmsg("could not coerce ON EMPTY expression (%s) to the RETURNING type",
+           /*- translator: first %s is a SQL/JSON clause (e.g. ON ERROR) */
+                    errmsg("could not coerce %s expression (%s) to the RETURNING type",
+                           "ON EMPTY",
                            GetJsonBehaviorValueString(jsestate->jsexpr->on_empty)),
                     errdetail("%s", jsestate->escontext.error_data->message)));
 
index d2db69a3f9017853878f465c41afbad4074aef40..56e413da9f51f6c802ed40612d091df0cb9b8306 100644 (file)
@@ -1099,7 +1099,8 @@ transformAExprNullIf(ParseState *pstate, A_Expr *a)
    if (result->opresulttype != BOOLOID)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
-                errmsg("NULLIF requires = operator to yield boolean"),
+       /* translator: %s is name of a SQL construct, eg NULLIF */
+                errmsg("%s requires = operator to yield boolean", "NULLIF"),
                 parser_errposition(pstate, a->location)));
    if (result->opretset)
        ereport(ERROR,
@@ -3060,7 +3061,9 @@ make_distinct_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree,
    if (((OpExpr *) result)->opresulttype != BOOLOID)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
-                errmsg("IS DISTINCT FROM requires = operator to yield boolean"),
+       /* translator: %s is name of a SQL construct, eg NULLIF */
+                errmsg("%s requires = operator to yield boolean",
+                       "IS DISTINCT FROM"),
                 parser_errposition(pstate, location)));
    if (((OpExpr *) result)->opretset)
        ereport(ERROR,
@@ -4326,15 +4329,22 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
            if (func->column_name == NULL)
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON EMPTY behavior"),
-                       errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for JSON_QUERY()."),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior", "ON EMPTY"),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
+                   second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
+                       errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
+                                 "ON EMPTY", "JSON_QUERY()"),
                        parser_errposition(pstate, func->on_empty->location));
            else
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON EMPTY behavior for column \"%s\"",
-                              func->column_name),
-                       errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON EMPTY for formatted columns."),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior for column \"%s\"",
+                              "ON EMPTY", func->column_name),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
+                                 "ON EMPTY"),
                        parser_errposition(pstate, func->on_empty->location));
        }
        if (func->on_error != NULL &&
@@ -4348,15 +4358,22 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
            if (func->column_name == NULL)
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON ERROR behavior"),
-                       errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for JSON_QUERY()."),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior", "ON ERROR"),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
+                   second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
+                       errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for %s.",
+                                 "ON ERROR", "JSON_QUERY()"),
                        parser_errposition(pstate, func->on_error->location));
            else
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON ERROR behavior for column \"%s\"",
-                              func->column_name),
-                       errdetail("Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for formatted columns."),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior for column \"%s\"",
+                              "ON ERROR", func->column_name),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errdetail("Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in %s for formatted columns.",
+                                 "ON ERROR"),
                        parser_errposition(pstate, func->on_error->location));
        }
    }
@@ -4372,15 +4389,20 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
        if (func->column_name == NULL)
            ereport(ERROR,
                    errcode(ERRCODE_SYNTAX_ERROR),
-                   errmsg("invalid ON ERROR behavior"),
-                   errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for JSON_EXISTS()."),
+           /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                   errmsg("invalid %s behavior", "ON ERROR"),
+                   errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for %s.",
+                             "ON ERROR", "JSON_EXISTS()"),
                    parser_errposition(pstate, func->on_error->location));
        else
            ereport(ERROR,
                    errcode(ERRCODE_SYNTAX_ERROR),
-                   errmsg("invalid ON ERROR behavior for column \"%s\"",
-                          func->column_name),
-                   errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for EXISTS columns."),
+           /*- translator: first %s is name a SQL/JSON clause (eg. ON EMPTY) */
+                   errmsg("invalid %s behavior for column \"%s\"",
+                          "ON ERROR", func->column_name),
+           /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                   errdetail("Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in %s for EXISTS columns.",
+                             "ON ERROR"),
                    parser_errposition(pstate, func->on_error->location));
    }
    if (func->op == JSON_VALUE_OP)
@@ -4393,15 +4415,22 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
            if (func->column_name == NULL)
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON EMPTY behavior"),
-                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for JSON_VALUE()."),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior", "ON EMPTY"),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
+                   second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
+                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
+                                 "ON EMPTY", "JSON_VALUE()"),
                        parser_errposition(pstate, func->on_empty->location));
            else
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON EMPTY behavior for column \"%s\"",
-                              func->column_name),
-                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON EMPTY for scalar columns."),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior for column \"%s\"",
+                              "ON EMPTY", func->column_name),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
+                                 "ON EMPTY"),
                        parser_errposition(pstate, func->on_empty->location));
        }
        if (func->on_error != NULL &&
@@ -4412,15 +4441,22 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
            if (func->column_name == NULL)
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON ERROR behavior"),
-                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for JSON_VALUE()."),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior", "ON ERROR"),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY),
+                   second %s is a SQL/JSON function name (e.g. JSON_QUERY) */
+                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for %s.",
+                                 "ON ERROR", "JSON_VALUE()"),
                        parser_errposition(pstate, func->on_error->location));
            else
                ereport(ERROR,
                        errcode(ERRCODE_SYNTAX_ERROR),
-                       errmsg("invalid ON ERROR behavior for column \"%s\"",
-                              func->column_name),
-                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in ON ERROR for scalar columns."),
+               /*- translator: first %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errmsg("invalid %s behavior for column \"%s\"",
+                              "ON ERROR", func->column_name),
+               /*- translator: %s is name of a SQL/JSON clause (eg. ON EMPTY) */
+                       errdetail("Only ERROR, NULL, or DEFAULT expression is allowed in %s for scalar columns.",
+                                 "ON ERROR"),
                        parser_errposition(pstate, func->on_error->location));
        }
    }
index 8a72e498e89331b4db1cc6ec282d5f0fc227733a..6d4e4968202e7962f363410596d7bbcc2e071174 100644 (file)
@@ -91,7 +91,7 @@ transformJsonTable(ParseState *pstate, JsonTable *jt)
        jt->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
        ereport(ERROR,
                errcode(ERRCODE_SYNTAX_ERROR),
-               errmsg("invalid ON ERROR behavior"),
+               errmsg("invalid %s behavior", "ON ERROR"),
                errdetail("Only EMPTY [ ARRAY ] or ERROR is allowed in the top-level ON ERROR clause."),
                parser_errposition(pstate, jt->on_error->location));
 
@@ -292,7 +292,7 @@ transformJsonTableColumns(JsonTableParseContext *cxt, List *columns,
                if (ordinality_found)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
-                            errmsg("cannot use more than one FOR ORDINALITY column"),
+                            errmsg("only one FOR ORDINALITY column is allowed"),
                             parser_errposition(pstate, rawc->location)));
                ordinality_found = true;
                colexpr = NULL;
index 9a97dd90d787f122b6d5e44b37999b74f7082263..ca8604a0538e2b74d638db0b138eb84921e18311 100644 (file)
@@ -489,7 +489,7 @@ DROP VIEW jsonb_table_view6;
 DROP DOMAIN jsonb_test_domain;
 -- JSON_TABLE: only one FOR ORDINALITY columns allowed
 SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (id FOR ORDINALITY, id2 FOR ORDINALITY, a int PATH '$.a' ERROR ON EMPTY)) jt;
-ERROR:  cannot use more than one FOR ORDINALITY column
+ERROR:  only one FOR ORDINALITY column is allowed
 LINE 1: ..._TABLE(jsonb '1', '$' COLUMNS (id FOR ORDINALITY, id2 FOR OR...
                                                              ^
 SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (id FOR ORDINALITY, a int PATH '$' ERROR ON EMPTY)) jt;
@@ -1126,7 +1126,7 @@ SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int omit quotes true on error
 ERROR:  invalid ON ERROR behavior for column "a"
 LINE 1: ...N_TABLE(jsonb '1', '$' COLUMNS (a int omit quotes true on er...
                                                              ^
-DETAIL:  Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for formatted columns.
+DETAIL:  Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for formatted columns.
 SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on error));
 ERROR:  invalid ON ERROR behavior for column "a"
 LINE 1: ...M JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty obje...
index 52b0b029ff7552a0135fa963f09369ccc083edaf..8b64e3c06b3b7b2d4659d0468562876ecfc880e9 100644 (file)
@@ -1384,7 +1384,7 @@ SELECT JSON_QUERY(jsonb '1', '$' TRUE ON ERROR);
 ERROR:  invalid ON ERROR behavior
 LINE 1: SELECT JSON_QUERY(jsonb '1', '$' TRUE ON ERROR);
                                          ^
-DETAIL:  Only ERROR, NULL, EMPTY [ ARRAY ], EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for JSON_QUERY().
+DETAIL:  Only ERROR, NULL, EMPTY ARRAY, EMPTY OBJECT, or DEFAULT expression is allowed in ON ERROR for JSON_QUERY().
 -- Test implicit coercion to a domain over fixed-length type specified in
 -- RETURNING
 CREATE DOMAIN queryfuncs_char2 AS char(2);