v3-0001-Add-error-hints-for-invalid-binary-encoding-names.patch
application/octet-stream
Filename: v3-0001-Add-error-hints-for-invalid-binary-encoding-names.patch
Type: application/octet-stream
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v3-0001
Subject: Add error hints for invalid binary encoding names
| File | + | − |
|---|---|---|
| src/backend/utils/adt/encode.c | 6 | 2 |
| src/test/regress/expected/strings.out | 7 | 0 |
| src/test/regress/sql/strings.sql | 4 | 0 |
From 7eaba1e603a3b6c45e4cb6f3ae25516bec8d3c7d Mon Sep 17 00:00:00 2001
From: Shinya Sugamoto <sugamoto@me.com>
Date: Mon, 17 Nov 2025 22:56:07 +0900
Subject: [PATCH] Add error hints for invalid binary encoding names
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
When users provide an invalid encoding name to encode() or decode()
functions, display a helpful error hint listing the valid options:
"hex", "base64", "base64url", and "escape".
This improvement helps users immediately see the supported encoding
options without needing to consult documentation.
---
src/backend/utils/adt/encode.c | 8 ++++++--
src/test/regress/expected/strings.out | 7 +++++++
src/test/regress/sql/strings.sql | 4 ++++
3 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index aabe9913eee..c813ee1258b 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -64,7 +64,9 @@ binary_encode(PG_FUNCTION_ARGS)
if (enc == NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("unrecognized encoding: \"%s\"", namebuf)));
+ errmsg("unrecognized encoding: \"%s\"", namebuf),
+ errhint("Valid encodings are \"%s\", \"%s\", \"%s\", and \"%s\".",
+ "base64", "base64url", "escape", "hex")));
dataptr = VARDATA_ANY(data);
datalen = VARSIZE_ANY_EXHDR(data);
@@ -112,7 +114,9 @@ binary_decode(PG_FUNCTION_ARGS)
if (enc == NULL)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("unrecognized encoding: \"%s\"", namebuf)));
+ errmsg("unrecognized encoding: \"%s\"", namebuf),
+ errhint("Valid encodings are \"%s\", \"%s\", \"%s\", and \"%s\".",
+ "base64", "base64url", "escape", "hex")));
dataptr = VARDATA_ANY(data);
datalen = VARSIZE_ANY_EXHDR(data);
diff --git a/src/test/regress/expected/strings.out b/src/test/regress/expected/strings.out
index b9dc08d5f61..727304f60e7 100644
--- a/src/test/regress/expected/strings.out
+++ b/src/test/regress/expected/strings.out
@@ -2575,6 +2575,13 @@ SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
\x1234567890abcdef00
(1 row)
+-- report an error with a hint listing valid encodings when an invalid encoding is specified
+SELECT encode('\x01'::bytea, 'invalid'); -- error
+ERROR: unrecognized encoding: "invalid"
+HINT: Valid encodings are "base64", "base64url", "escape", and "hex".
+SELECT decode('00', 'invalid'); -- error
+ERROR: unrecognized encoding: "invalid"
+HINT: Valid encodings are "base64", "base64url", "escape", and "hex".
--
-- base64url encoding/decoding
--
diff --git a/src/test/regress/sql/strings.sql b/src/test/regress/sql/strings.sql
index a2a91523404..88aa4c2983b 100644
--- a/src/test/regress/sql/strings.sql
+++ b/src/test/regress/sql/strings.sql
@@ -815,6 +815,10 @@ SELECT decode(encode(('\x' || repeat('1234567890abcdef0001', 7))::bytea,
SELECT encode('\x1234567890abcdef00', 'escape');
SELECT decode(encode('\x1234567890abcdef00', 'escape'), 'escape');
+-- report an error with a hint listing valid encodings when an invalid encoding is specified
+SELECT encode('\x01'::bytea, 'invalid'); -- error
+SELECT decode('00', 'invalid'); -- error
+
--
-- base64url encoding/decoding
--
--
2.50.1 (Apple Git-155)