diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-11 11:28:15 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-11 11:28:15 -0500 |
commit | c60c9badba9b4db3155eef081ae0b923f983168c (patch) | |
tree | 60b12d49fa9f01feff7707cef18fac7ebf24ab18 /src/common/jsonapi.c | |
parent | 50428a301d5ad46316cac2192f2ca8d91898aa3c (diff) | |
download | postgresql-c60c9badba9b4db3155eef081ae0b923f983168c.tar.gz postgresql-c60c9badba9b4db3155eef081ae0b923f983168c.zip |
Convert json_in and jsonb_in to report errors softly.
This requires a bit of further infrastructure-extension to allow
trapping errors reported by numeric_in and pg_unicode_to_server,
but otherwise it's pretty straightforward.
In the case of jsonb_in, we are only capturing errors reported
during the initial "parse" phase. The value-construction phase
(JsonbValueToJsonb) can also throw errors if assorted implementation
limits are exceeded. We should improve that, but it seems like a
separable project.
Andrew Dunstan and Tom Lane
Discussion: https://postgr.es/m/3bac9841-fe07-713d-fa42-606c225567d6@dunslane.net
Diffstat (limited to 'src/common/jsonapi.c')
-rw-r--r-- | src/common/jsonapi.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/src/common/jsonapi.c b/src/common/jsonapi.c index 83c286b89ba..773ae8074ad 100644 --- a/src/common/jsonapi.c +++ b/src/common/jsonapi.c @@ -791,19 +791,16 @@ json_lex_string(JsonLexContext *lex) /* * Add the represented character to lex->strval. In the - * backend, we can let pg_unicode_to_server() handle any - * required character set conversion; in frontend, we can - * only deal with trivial conversions. - * - * Note: pg_unicode_to_server() will throw an error for a - * conversion failure, rather than returning a failure - * indication. That seems OK. + * backend, we can let pg_unicode_to_server_noerror() + * handle any required character set conversion; in + * frontend, we can only deal with trivial conversions. */ #ifndef FRONTEND { char cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1]; - pg_unicode_to_server(ch, (unsigned char *) cbuf); + if (!pg_unicode_to_server_noerror(ch, (unsigned char *) cbuf)) + return JSON_UNICODE_UNTRANSLATABLE; appendStringInfoString(lex->strval, cbuf); } #else @@ -1167,6 +1164,10 @@ json_errdetail(JsonParseErrorType error, JsonLexContext *lex) case JSON_UNICODE_HIGH_ESCAPE: /* note: this case is only reachable in frontend not backend */ return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8."); + case JSON_UNICODE_UNTRANSLATABLE: + /* note: this case is only reachable in backend not frontend */ + return psprintf(_("Unicode escape value could not be translated to the server's encoding %s."), + GetDatabaseEncodingName()); case JSON_UNICODE_HIGH_SURROGATE: return _("Unicode high surrogate must not follow a high surrogate."); case JSON_UNICODE_LOW_SURROGATE: |