Commit 80edfd76 authored by Tom Lane's avatar Tom Lane

Revisit error message details for JSON input parsing.

Instead of identifying error locations only by line number (which could
be entirely unhelpful with long input lines), provide a fragment of the
input text too, placing this info in a new CONTEXT entry.  Make the
error detail messages conform more closely to style guidelines, fix
failure to expose some of them for translation, ensure compiler can
check formats against supplied parameters.
parent 0f0fba17
This diff is collapsed.
...@@ -9,7 +9,8 @@ SELECT $$''$$::json; -- ERROR, single quotes are not allowed ...@@ -9,7 +9,8 @@ SELECT $$''$$::json; -- ERROR, single quotes are not allowed
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT $$''$$::json; LINE 1: SELECT $$''$$::json;
^ ^
DETAIL: line 1: Token "'" is invalid. DETAIL: Token "'" is invalid.
CONTEXT: JSON data, line 1: '...
SELECT '"abc"'::json; -- OK SELECT '"abc"'::json; -- OK
json json
------- -------
...@@ -20,13 +21,15 @@ SELECT '"abc'::json; -- ERROR, quotes not closed ...@@ -20,13 +21,15 @@ SELECT '"abc'::json; -- ERROR, quotes not closed
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"abc'::json; LINE 1: SELECT '"abc'::json;
^ ^
DETAIL: line 1: Token ""abc" is invalid. DETAIL: Token ""abc" is invalid.
CONTEXT: JSON data, line 1: "abc
SELECT '"abc SELECT '"abc
def"'::json; -- ERROR, unescaped newline in string constant def"'::json; -- ERROR, unescaped newline in string constant
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"abc LINE 1: SELECT '"abc
^ ^
DETAIL: line 1: Character with value "0x0a" must be escaped. DETAIL: Character with value 0x0a must be escaped.
CONTEXT: JSON data, line 1: "abc
SELECT '"\n\"\\"'::json; -- OK, legal escapes SELECT '"\n\"\\"'::json; -- OK, legal escapes
json json
---------- ----------
...@@ -37,22 +40,26 @@ SELECT '"\v"'::json; -- ERROR, not a valid JSON escape ...@@ -37,22 +40,26 @@ SELECT '"\v"'::json; -- ERROR, not a valid JSON escape
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"\v"'::json; LINE 1: SELECT '"\v"'::json;
^ ^
DETAIL: line 1: Invalid escape "\v". DETAIL: Escape sequence "\v" is invalid.
CONTEXT: JSON data, line 1: "\v...
SELECT '"\u"'::json; -- ERROR, incomplete escape SELECT '"\u"'::json; -- ERROR, incomplete escape
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"\u"'::json; LINE 1: SELECT '"\u"'::json;
^ ^
DETAIL: line 1: "\u" must be followed by four hexadecimal digits. DETAIL: "\u" must be followed by four hexadecimal digits.
CONTEXT: JSON data, line 1: "\u"
SELECT '"\u00"'::json; -- ERROR, incomplete escape SELECT '"\u00"'::json; -- ERROR, incomplete escape
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"\u00"'::json; LINE 1: SELECT '"\u00"'::json;
^ ^
DETAIL: line 1: "\u" must be followed by four hexadecimal digits. DETAIL: "\u" must be followed by four hexadecimal digits.
CONTEXT: JSON data, line 1: "\u00"
SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit SELECT '"\u000g"'::json; -- ERROR, g is not a hex digit
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '"\u000g"'::json; LINE 1: SELECT '"\u000g"'::json;
^ ^
DETAIL: line 1: "\u" must be followed by four hexadecimal digits. DETAIL: "\u" must be followed by four hexadecimal digits.
CONTEXT: JSON data, line 1: "\u000g...
SELECT '"\u0000"'::json; -- OK, legal escape SELECT '"\u0000"'::json; -- OK, legal escape
json json
---------- ----------
...@@ -82,7 +89,8 @@ SELECT '01'::json; -- ERROR, not valid according to JSON spec ...@@ -82,7 +89,8 @@ SELECT '01'::json; -- ERROR, not valid according to JSON spec
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '01'::json; LINE 1: SELECT '01'::json;
^ ^
DETAIL: line 1: Token "01" is invalid. DETAIL: Token "01" is invalid.
CONTEXT: JSON data, line 1: 01
SELECT '0.1'::json; -- OK SELECT '0.1'::json; -- OK
json json
------ ------
...@@ -111,17 +119,20 @@ SELECT '1f2'::json; -- ERROR ...@@ -111,17 +119,20 @@ SELECT '1f2'::json; -- ERROR
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '1f2'::json; LINE 1: SELECT '1f2'::json;
^ ^
DETAIL: line 1: Token "1f2" is invalid. DETAIL: Token "1f2" is invalid.
CONTEXT: JSON data, line 1: 1f2
SELECT '0.x1'::json; -- ERROR SELECT '0.x1'::json; -- ERROR
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '0.x1'::json; LINE 1: SELECT '0.x1'::json;
^ ^
DETAIL: line 1: Token "0.x1" is invalid. DETAIL: Token "0.x1" is invalid.
CONTEXT: JSON data, line 1: 0.x1
SELECT '1.3ex100'::json; -- ERROR SELECT '1.3ex100'::json; -- ERROR
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '1.3ex100'::json; LINE 1: SELECT '1.3ex100'::json;
^ ^
DETAIL: line 1: Token "1.3ex100" is invalid. DETAIL: Token "1.3ex100" is invalid.
CONTEXT: JSON data, line 1: 1.3ex100
-- Arrays. -- Arrays.
SELECT '[]'::json; -- OK SELECT '[]'::json; -- OK
json json
...@@ -142,20 +153,23 @@ SELECT '[1,2]'::json; -- OK ...@@ -142,20 +153,23 @@ SELECT '[1,2]'::json; -- OK
(1 row) (1 row)
SELECT '[1,2,]'::json; -- ERROR, trailing comma SELECT '[1,2,]'::json; -- ERROR, trailing comma
ERROR: invalid input syntax for type json: "[1,2,]" ERROR: invalid input syntax for type json
LINE 1: SELECT '[1,2,]'::json; LINE 1: SELECT '[1,2,]'::json;
^ ^
DETAIL: line 1: Expected string, number, object, array, true, false, or null, but found "]". DETAIL: Expected JSON value, but found "]".
CONTEXT: JSON data, line 1: [1,2,]
SELECT '[1,2'::json; -- ERROR, no closing bracket SELECT '[1,2'::json; -- ERROR, no closing bracket
ERROR: invalid input syntax for type json: "[1,2" ERROR: invalid input syntax for type json
LINE 1: SELECT '[1,2'::json; LINE 1: SELECT '[1,2'::json;
^ ^
DETAIL: The input string ended unexpectedly. DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1: [1,2
SELECT '[1,[2]'::json; -- ERROR, no closing bracket SELECT '[1,[2]'::json; -- ERROR, no closing bracket
ERROR: invalid input syntax for type json: "[1,[2]" ERROR: invalid input syntax for type json
LINE 1: SELECT '[1,[2]'::json; LINE 1: SELECT '[1,[2]'::json;
^ ^
DETAIL: The input string ended unexpectedly. DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1: [1,[2]
-- Objects. -- Objects.
SELECT '{}'::json; -- OK SELECT '{}'::json; -- OK
json json
...@@ -164,10 +178,11 @@ SELECT '{}'::json; -- OK ...@@ -164,10 +178,11 @@ SELECT '{}'::json; -- OK
(1 row) (1 row)
SELECT '{"abc"}'::json; -- ERROR, no value SELECT '{"abc"}'::json; -- ERROR, no value
ERROR: invalid input syntax for type json: "{"abc"}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc"}'::json; LINE 1: SELECT '{"abc"}'::json;
^ ^
DETAIL: line 1: Expected ":", but found "}". DETAIL: Expected ":", but found "}".
CONTEXT: JSON data, line 1: {"abc"}
SELECT '{"abc":1}'::json; -- OK SELECT '{"abc":1}'::json; -- OK
json json
----------- -----------
...@@ -175,25 +190,29 @@ SELECT '{"abc":1}'::json; -- OK ...@@ -175,25 +190,29 @@ SELECT '{"abc":1}'::json; -- OK
(1 row) (1 row)
SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings SELECT '{1:"abc"}'::json; -- ERROR, keys must be strings
ERROR: invalid input syntax for type json: "{1:"abc"}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{1:"abc"}'::json; LINE 1: SELECT '{1:"abc"}'::json;
^ ^
DETAIL: line 1: Expected string or "}", but found "1". DETAIL: Expected string or "}", but found "1".
CONTEXT: JSON data, line 1: {1...
SELECT '{"abc",1}'::json; -- ERROR, wrong separator SELECT '{"abc",1}'::json; -- ERROR, wrong separator
ERROR: invalid input syntax for type json: "{"abc",1}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc",1}'::json; LINE 1: SELECT '{"abc",1}'::json;
^ ^
DETAIL: line 1: Expected ":", but found ",". DETAIL: Expected ":", but found ",".
CONTEXT: JSON data, line 1: {"abc",...
SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator SELECT '{"abc"=1}'::json; -- ERROR, totally wrong separator
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc"=1}'::json; LINE 1: SELECT '{"abc"=1}'::json;
^ ^
DETAIL: line 1: Token "=" is invalid. DETAIL: Token "=" is invalid.
CONTEXT: JSON data, line 1: {"abc"=...
SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator SELECT '{"abc"::1}'::json; -- ERROR, another wrong separator
ERROR: invalid input syntax for type json: "{"abc"::1}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc"::1}'::json; LINE 1: SELECT '{"abc"::1}'::json;
^ ^
DETAIL: line 1: Expected string, number, object, array, true, false, or null, but found ":". DETAIL: Expected JSON value, but found ":".
CONTEXT: JSON data, line 1: {"abc"::...
SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
json json
--------------------------------------------------------- ---------------------------------------------------------
...@@ -201,15 +220,17 @@ SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK ...@@ -201,15 +220,17 @@ SELECT '{"abc":1,"def":2,"ghi":[3,4],"hij":{"klm":5,"nop":[6]}}'::json; -- OK
(1 row) (1 row)
SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot SELECT '{"abc":1:2}'::json; -- ERROR, colon in wrong spot
ERROR: invalid input syntax for type json: "{"abc":1:2}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc":1:2}'::json; LINE 1: SELECT '{"abc":1:2}'::json;
^ ^
DETAIL: line 1: Expected "," or "}", but found ":". DETAIL: Expected "," or "}", but found ":".
CONTEXT: JSON data, line 1: {"abc":1:...
SELECT '{"abc":1,3}'::json; -- ERROR, no value SELECT '{"abc":1,3}'::json; -- ERROR, no value
ERROR: invalid input syntax for type json: "{"abc":1,3}" ERROR: invalid input syntax for type json
LINE 1: SELECT '{"abc":1,3}'::json; LINE 1: SELECT '{"abc":1,3}'::json;
^ ^
DETAIL: line 1: Expected string, but found "3". DETAIL: Expected string, but found "3".
CONTEXT: JSON data, line 1: {"abc":1,3...
-- Miscellaneous stuff. -- Miscellaneous stuff.
SELECT 'true'::json; -- OK SELECT 'true'::json; -- OK
json json
...@@ -236,35 +257,41 @@ SELECT ' true '::json; -- OK, even with extra whitespace ...@@ -236,35 +257,41 @@ SELECT ' true '::json; -- OK, even with extra whitespace
(1 row) (1 row)
SELECT 'true false'::json; -- ERROR, too many values SELECT 'true false'::json; -- ERROR, too many values
ERROR: invalid input syntax for type json: "true false" ERROR: invalid input syntax for type json
LINE 1: SELECT 'true false'::json; LINE 1: SELECT 'true false'::json;
^ ^
DETAIL: line 1: Expected end of input, but found "false". DETAIL: Expected end of input, but found "false".
CONTEXT: JSON data, line 1: true false
SELECT 'true, false'::json; -- ERROR, too many values SELECT 'true, false'::json; -- ERROR, too many values
ERROR: invalid input syntax for type json: "true, false" ERROR: invalid input syntax for type json
LINE 1: SELECT 'true, false'::json; LINE 1: SELECT 'true, false'::json;
^ ^
DETAIL: line 1: Expected end of input, but found ",". DETAIL: Expected end of input, but found ",".
CONTEXT: JSON data, line 1: true,...
SELECT 'truf'::json; -- ERROR, not a keyword SELECT 'truf'::json; -- ERROR, not a keyword
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT 'truf'::json; LINE 1: SELECT 'truf'::json;
^ ^
DETAIL: line 1: Token "truf" is invalid. DETAIL: Token "truf" is invalid.
CONTEXT: JSON data, line 1: truf
SELECT 'trues'::json; -- ERROR, not a keyword SELECT 'trues'::json; -- ERROR, not a keyword
ERROR: invalid input syntax for type json ERROR: invalid input syntax for type json
LINE 1: SELECT 'trues'::json; LINE 1: SELECT 'trues'::json;
^ ^
DETAIL: line 1: Token "trues" is invalid. DETAIL: Token "trues" is invalid.
CONTEXT: JSON data, line 1: trues
SELECT ''::json; -- ERROR, no value SELECT ''::json; -- ERROR, no value
ERROR: invalid input syntax for type json: "" ERROR: invalid input syntax for type json
LINE 1: SELECT ''::json; LINE 1: SELECT ''::json;
^ ^
DETAIL: The input string ended unexpectedly. DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1:
SELECT ' '::json; -- ERROR, no value SELECT ' '::json; -- ERROR, no value
ERROR: invalid input syntax for type json: " " ERROR: invalid input syntax for type json
LINE 1: SELECT ' '::json; LINE 1: SELECT ' '::json;
^ ^
DETAIL: The input string ended unexpectedly. DETAIL: The input string ended unexpectedly.
CONTEXT: JSON data, line 1:
--constructors --constructors
-- array_to_json -- array_to_json
SELECT array_to_json(array(select 1 as a)); SELECT array_to_json(array(select 1 as a));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment