🌐 AI搜索 & 代理 主页
Skip to content

Commit f7fbd02

Browse files
committed
libpq: Align oauth_json_set_error() with other NLS patterns
Now that the prior commits have fixed missing OAuth translations, pull the bespoke usage of libpq_gettext() for OAUTHBEARER parsing into oauth_json_set_error() itself, and make that a gettext trigger as well, to better match what the other sites are doing. Add an _internal() variant to handle the existing untranslated case. Suggested-by: Daniel Gustafsson <daniel@yesql.se> Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de> Discussion: https://postgr.es/m/0EEBCAA8-A5AC-4E3B-BABA-0BA7A08C361B%40yesql.se Backpatch-through: 18
1 parent 301a1dc commit f7fbd02

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/interfaces/libpq/fe-auth-oauth.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,14 @@ struct json_ctx
183183
#define oauth_json_has_error(ctx) \
184184
(PQExpBufferDataBroken((ctx)->errbuf) || (ctx)->errmsg)
185185

186-
#define oauth_json_set_error(ctx, ...) \
186+
#define oauth_json_set_error(ctx, fmt, ...) \
187+
do { \
188+
appendPQExpBuffer(&(ctx)->errbuf, libpq_gettext(fmt), ##__VA_ARGS__); \
189+
(ctx)->errmsg = (ctx)->errbuf.data; \
190+
} while (0)
191+
192+
/* An untranslated version of oauth_json_set_error(). */
193+
#define oauth_json_set_error_internal(ctx, ...) \
187194
do { \
188195
appendPQExpBuffer(&(ctx)->errbuf, __VA_ARGS__); \
189196
(ctx)->errmsg = (ctx)->errbuf.data; \
@@ -199,13 +206,13 @@ oauth_json_object_start(void *state)
199206
Assert(ctx->nested == 1);
200207

201208
oauth_json_set_error(ctx,
202-
libpq_gettext("field \"%s\" must be a string"),
209+
"field \"%s\" must be a string",
203210
ctx->target_field_name);
204211
}
205212

206213
++ctx->nested;
207214
if (ctx->nested > MAX_SASL_NESTING_LEVEL)
208-
oauth_json_set_error(ctx, libpq_gettext("JSON is too deeply nested"));
215+
oauth_json_set_error(ctx, "JSON is too deeply nested");
209216

210217
return oauth_json_has_error(ctx) ? JSON_SEM_ACTION_FAILED : JSON_SUCCESS;
211218
}
@@ -254,20 +261,20 @@ oauth_json_array_start(void *state)
254261

255262
if (!ctx->nested)
256263
{
257-
ctx->errmsg = libpq_gettext("top-level element must be an object");
264+
oauth_json_set_error(ctx, "top-level element must be an object");
258265
}
259266
else if (ctx->target_field)
260267
{
261268
Assert(ctx->nested == 1);
262269

263270
oauth_json_set_error(ctx,
264-
libpq_gettext("field \"%s\" must be a string"),
271+
"field \"%s\" must be a string",
265272
ctx->target_field_name);
266273
}
267274

268275
++ctx->nested;
269276
if (ctx->nested > MAX_SASL_NESTING_LEVEL)
270-
oauth_json_set_error(ctx, libpq_gettext("JSON is too deeply nested"));
277+
oauth_json_set_error(ctx, "JSON is too deeply nested");
271278

272279
return oauth_json_has_error(ctx) ? JSON_SEM_ACTION_FAILED : JSON_SUCCESS;
273280
}
@@ -288,7 +295,7 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
288295

289296
if (!ctx->nested)
290297
{
291-
ctx->errmsg = libpq_gettext("top-level element must be an object");
298+
oauth_json_set_error(ctx, "top-level element must be an object");
292299
return JSON_SEM_ACTION_FAILED;
293300
}
294301

@@ -301,9 +308,9 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
301308
* Assert and don't continue any further for production builds.
302309
*/
303310
Assert(false);
304-
oauth_json_set_error(ctx,
305-
"internal error: target scalar found at nesting level %d during OAUTHBEARER parsing",
306-
ctx->nested);
311+
oauth_json_set_error_internal(ctx,
312+
"internal error: target scalar found at nesting level %d during OAUTHBEARER parsing",
313+
ctx->nested);
307314
return JSON_SEM_ACTION_FAILED;
308315
}
309316

@@ -314,7 +321,7 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
314321
if (*ctx->target_field)
315322
{
316323
oauth_json_set_error(ctx,
317-
libpq_gettext("field \"%s\" is duplicated"),
324+
"field \"%s\" is duplicated",
318325
ctx->target_field_name);
319326
return JSON_SEM_ACTION_FAILED;
320327
}
@@ -323,7 +330,7 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
323330
if (type != JSON_TOKEN_STRING)
324331
{
325332
oauth_json_set_error(ctx,
326-
libpq_gettext("field \"%s\" must be a string"),
333+
"field \"%s\" must be a string",
327334
ctx->target_field_name);
328335
return JSON_SEM_ACTION_FAILED;
329336
}

src/interfaces/libpq/nls.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ GETTEXT_TRIGGERS = actx_error:2 \
2222
libpq_append_error:2 \
2323
libpq_gettext \
2424
libpq_ngettext:1,2 \
25+
oauth_json_set_error:2 \
2526
oauth_parse_set_error:2 \
2627
pqInternalNotice:2
2728
GETTEXT_FLAGS = actx_error:2:c-format \
@@ -30,5 +31,6 @@ GETTEXT_FLAGS = actx_error:2:c-format \
3031
libpq_gettext:1:pass-c-format \
3132
libpq_ngettext:1:pass-c-format \
3233
libpq_ngettext:2:pass-c-format \
34+
oauth_json_set_error:2:c-format \
3335
oauth_parse_set_error:2:c-format \
3436
pqInternalNotice:2:c-format

0 commit comments

Comments
 (0)