Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1548c78

Browse files
committed
Code review for error reports in jsonb_set().
User-facing (even tested by regression tests) error conditions were thrownwith elog(), hence had wrong SQLSTATE and were untranslatable. And theerror message texts weren't up to project style, either.
1 parent5f95521 commit1548c78

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

‎src/backend/utils/adt/jsonfuncs.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3722,12 +3722,15 @@ setPath(JsonbIterator **it, Datum *path_elems,
37223722
{
37233723
JsonbValuev;
37243724
JsonbIteratorTokenr;
3725-
JsonbValue*res=NULL;
3725+
JsonbValue*res;
37263726

37273727
check_stack_depth();
37283728

37293729
if (path_nulls[level])
3730-
elog(ERROR,"path element at the position %d is NULL",level+1);
3730+
ereport(ERROR,
3731+
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3732+
errmsg("path element at position %d is null",
3733+
level+1)));
37313734

37323735
r=JsonbIteratorNext(it,&v, false);
37333736

@@ -3740,7 +3743,6 @@ setPath(JsonbIterator **it, Datum *path_elems,
37403743
r=JsonbIteratorNext(it,&v, false);
37413744
Assert(r==WJB_END_ARRAY);
37423745
res=pushJsonbValue(st,r,NULL);
3743-
37443746
break;
37453747
caseWJB_BEGIN_OBJECT:
37463748
(void)pushJsonbValue(st,r,NULL);
@@ -3749,14 +3751,15 @@ setPath(JsonbIterator **it, Datum *path_elems,
37493751
r=JsonbIteratorNext(it,&v, true);
37503752
Assert(r==WJB_END_OBJECT);
37513753
res=pushJsonbValue(st,r,NULL);
3752-
37533754
break;
37543755
caseWJB_ELEM:
37553756
caseWJB_VALUE:
37563757
res=pushJsonbValue(st,r,&v);
37573758
break;
37583759
default:
3759-
elog(ERROR,"impossible state");
3760+
elog(ERROR,"unrecognized iterator result: %d", (int)r);
3761+
res=NULL;/* keep compiler quiet */
3762+
break;
37603763
}
37613764

37623765
returnres;
@@ -3867,22 +3870,24 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
38673870
JsonbValuev;
38683871
intidx,
38693872
i;
3870-
char*badp;
38713873
booldone= false;
38723874

38733875
/* pick correct index */
38743876
if (level<path_len&& !path_nulls[level])
38753877
{
38763878
char*c=TextDatumGetCString(path_elems[level]);
38773879
longlindex;
3880+
char*badp;
38783881

38793882
errno=0;
38803883
lindex=strtol(c,&badp,10);
38813884
if (errno!=0||badp==c||*badp!='\0'||lindex>INT_MAX||
38823885
lindex<INT_MIN)
3883-
elog(ERROR,"path element at the position %d is not an integer",level+1);
3884-
else
3885-
idx=lindex;
3886+
ereport(ERROR,
3887+
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
3888+
errmsg("path element at position %d is not an integer: \"%s\"",
3889+
level+1,c)));
3890+
idx=lindex;
38863891
}
38873892
else
38883893
idx=nelems;
@@ -3957,7 +3962,6 @@ setPathArray(JsonbIterator **it, Datum *path_elems, bool *path_nulls,
39573962
{
39583963
addJsonbToParseState(st,newval);
39593964
}
3960-
39613965
}
39623966
}
39633967
}

‎src/test/regress/expected/jsonb.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,7 +3114,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j
31143114
(1 row)
31153115

31163116
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '[1,2,3]');
3117-
ERROR: path element attheposition 2 isNULL
3117+
ERROR: path element at position 2 isnull
31183118
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '{"1": 2}');
31193119
jsonb_set
31203120
-------------------------------------------------------------------------
@@ -3134,7 +3134,7 @@ select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::j
31343134
(1 row)
31353135

31363136
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,NULL,0}', '{"1": 2}');
3137-
ERROR: path element attheposition 2 isNULL
3137+
ERROR: path element at position 2 isnull
31383138
select jsonb_set('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '"test"');
31393139
jsonb_set
31403140
--------------------------------------------------------------------------
@@ -3178,7 +3178,7 @@ select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{
31783178
(1 row)
31793179

31803180
select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{b,-1e}'; -- invalid array subscript
3181-
ERROR: path element attheposition 2 is not an integer
3181+
ERROR: path element at position 2 is not an integer: "-1e"
31823182
select '{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb #- '{d,1,0}';
31833183
?column?
31843184
------------------------------------------------------------------
@@ -3307,8 +3307,8 @@ select jsonb_set('[]','{-99}','{"foo":123}');
33073307
(1 row)
33083308

33093309
select jsonb_set('{"a": [1, 2, 3]}', '{a, non_integer}', '"new_value"');
3310-
ERROR: path element attheposition 2 is not an integer
3310+
ERROR: path element at position 2 is not an integer: "non_integer"
33113311
select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, non_integer}', '"new_value"');
3312-
ERROR: path element attheposition 3 is not an integer
3312+
ERROR: path element at position 3 is not an integer: "non_integer"
33133313
select jsonb_set('{"a": {"b": [1, 2, 3]}}', '{a, b, NULL}', '"new_value"');
3314-
ERROR: path element attheposition 3 isNULL
3314+
ERROR: path element at position 3 isnull

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp