The hstore module uses elog() to default to ERRCODE_INTERNAL_ERROR
(SQLSTATE XX000) when the error message reads "Syntax error near '%c' at
position %d".

I propose to switch to ereport() to return ERRCODE_SYNTAX_ERROR (SQLSTATE
42601), on the grounds that it's more transparent. It took me longer to
figure out what error code was being returned than to write both the patch
and my originally intended function using the patch.

I also propose to raise the same error code for "Unexpected end of string"
in hstore, since it serves the same purpose, at least in my mind:
differentiating between valid and invalid hstore syntax.

Any objections to my submitting the attached patch to the September
commitfest?
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 0c1d99a..0cbc865 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -81,7 +81,10 @@ get_val(HSParser *state, bool ignoreeq, bool *escaped)
                        }
                        else if (*(state->ptr) == '=' && !ignoreeq)
                        {
-                               elog(ERROR, "Syntax error near '%c' at position 
%d", *(state->ptr), (int32) (state->ptr - state->begin));
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Syntax error near 
'%c' at position %d",
+                                         *(state->ptr), (int32) (state->ptr - 
state->begin)))));
                        }
                        else if (*(state->ptr) == '\\')
                        {
@@ -138,7 +141,9 @@ get_val(HSParser *state, bool ignoreeq, bool *escaped)
                        }
                        else if (*(state->ptr) == '\0')
                        {
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        }
                        else
                        {
@@ -150,7 +155,9 @@ get_val(HSParser *state, bool ignoreeq, bool *escaped)
                else if (st == GV_WAITESCIN)
                {
                        if (*(state->ptr) == '\0')
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        RESIZEPRSBUF;
                        *(state->cur) = *(state->ptr);
                        state->cur++;
@@ -159,7 +166,9 @@ get_val(HSParser *state, bool ignoreeq, bool *escaped)
                else if (st == GV_WAITESCESCIN)
                {
                        if (*(state->ptr) == '\0')
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        RESIZEPRSBUF;
                        *(state->cur) = *(state->ptr);
                        state->cur++;
@@ -216,11 +225,16 @@ parse_hstore(HSParser *state)
                        }
                        else if (*(state->ptr) == '\0')
                        {
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        }
                        else if (!isspace((unsigned char) *(state->ptr)))
                        {
-                               elog(ERROR, "Syntax error near '%c' at position 
%d", *(state->ptr), (int32) (state->ptr - state->begin));
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Syntax error near 
'%c' at position %d",
+                                         *(state->ptr), (int32) (state->ptr - 
state->begin)))));
                        }
                }
                else if (st == WGT)
@@ -231,17 +245,24 @@ parse_hstore(HSParser *state)
                        }
                        else if (*(state->ptr) == '\0')
                        {
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        }
                        else
                        {
-                               elog(ERROR, "Syntax error near '%c' at position 
%d", *(state->ptr), (int32) (state->ptr - state->begin));
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Syntax error near 
'%c' at position %d",
+                                         *(state->ptr), (int32) (state->ptr - 
state->begin)))));
                        }
                }
                else if (st == WVAL)
                {
                        if (!get_val(state, true, &escaped))
-                               elog(ERROR, "Unexpected end of string");
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Unexpected end of 
string"))));
                        state->pairs[state->pcur].val = state->word;
                        state->pairs[state->pcur].vallen = 
hstoreCheckValLen(state->cur - state->word);
                        state->pairs[state->pcur].isnull = false;
@@ -268,7 +289,10 @@ parse_hstore(HSParser *state)
                        }
                        else if (!isspace((unsigned char) *(state->ptr)))
                        {
-                               elog(ERROR, "Syntax error near '%c' at position 
%d", *(state->ptr), (int32) (state->ptr - state->begin));
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_SYNTAX_ERROR),
+                                                (errmsg("Syntax error near 
'%c' at position %d",
+                                         *(state->ptr), (int32) (state->ptr - 
state->begin)))));
                        }
                }
                else

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to