Skip to content

Show position in syntax errors#521

Closed
fxkr wants to merge 1 commit intolib:masterfrom
fxkr:show-syntax-error-pos
Closed

Show position in syntax errors#521
fxkr wants to merge 1 commit intolib:masterfrom
fxkr:show-syntax-error-pos

Conversation

@fxkr
Copy link
Copy Markdown

@fxkr fxkr commented Oct 16, 2016

We now format them like psql, e.g.:

pq: syntax error at or near "FROM"
LINE 1: SELECT * FROM FROM items;
                      ^

Fixes #194
Fixes #679

We now format them like psql, e.g.:

    pq: syntax error at or near "FROM"
    LINE 1: SELECT * FROM FROM items;
                          ^

Closes lib#194
@arp242 arp242 mentioned this pull request Jan 3, 2026
arp242 added a commit that referenced this pull request Jan 3, 2026
…rors

Add a new ErrorWithDetail() method which also prints the Detail, Hint,
and query position (if any). For example:

    ERROR:  unacceptable schema name "pg_xx" (42939)
    DETAIL: The prefix "pg_" is reserved for system schemas.

Or if the Position is set and we have a query, or prints up to three
lines of context with a ^ to mark the position:

    ERROR:   invalid input syntax for type json (22P02)
    DETAIL:  Token "asd" is invalid.
    CONTEXT: line 5, column 8:

          3 | 'def',
          4 | 123,
          5 | 'foo', 'asd'::jsonb
                     ^

The format is based on what psql prints on errors (though not exactly
identical).

Based on the code I wrote for toml a few years ago:
https://github.com/BurntSushi/toml/blob/master/error.go

The regular error message now also includes the error code:

	pq: syntax error at end of input (42601)

psql doesn't print them, but I've found it useful on a number of
occasion, and I think in the context of a library like this it probably
makes sense.

Fixes #194
Fixes #521
Fixes #679
Fixes #895
arp242 added a commit that referenced this pull request Jan 3, 2026
Add a new ErrorWithDetail() method which also prints the Detail, Hint,
and query position (if any). For example:

    ERROR:  unacceptable schema name "pg_xx" (42939)
    DETAIL: The prefix "pg_" is reserved for system schemas.

Or if the Position is set and we have a query, or prints up to three
lines of context with a ^ to mark the position:

    ERROR:   invalid input syntax for type json (22P02)
    DETAIL:  Token "asd" is invalid.
    CONTEXT: line 5, column 8:

          3 | 'def',
          4 | 123,
          5 | 'foo', 'asd'::jsonb
                     ^

The format is based on what psql prints on errors (though not exactly
identical).

Based on the code I wrote for toml a few years ago:
https://github.com/BurntSushi/toml/blob/master/error.go

The regular error message now also includes the error code:

	pq: syntax error at end of input (42601)

psql doesn't print them, but I've found it useful on a number of
occasion, and I think in the context of a library like this it probably
makes sense.

Fixes #194
Fixes #521
Fixes #679
Fixes #895
arp242 added a commit that referenced this pull request Jan 3, 2026
Add a new ErrorWithDetail() method which also prints the Detail, Hint,
and query position (if any). For example:

    ERROR:  unacceptable schema name "pg_xx" (42939)
    DETAIL: The prefix "pg_" is reserved for system schemas.

Or if the Position is set and we have a query, or prints up to three
lines of context with a ^ to mark the position:

    ERROR:   invalid input syntax for type json (22P02)
    DETAIL:  Token "asd" is invalid.
    CONTEXT: line 5, column 8:

          3 | 'def',
          4 | 123,
          5 | 'foo', 'asd'::jsonb
                     ^

The format is based on what psql prints on errors (though not exactly
identical).

Based on the code I wrote for toml a few years ago:
https://github.com/BurntSushi/toml/blob/master/error.go

The regular error message now also includes the error code:

	pq: syntax error at end of input (42601)

psql doesn't print them, but I've found it useful on a number of
occasion, and I think in the context of a library like this it probably
makes sense.

Fixes #194
Fixes #521
Fixes #679
Fixes #895
arp242 added a commit that referenced this pull request Jan 3, 2026
Add a new ErrorWithDetail() method which also prints the Detail, Hint,
and query position (if any). For example:

    ERROR:  unacceptable schema name "pg_xx" (42939)
    DETAIL: The prefix "pg_" is reserved for system schemas.

Or if the Position is set and we have a query, or prints up to three
lines of context with a ^ to mark the position:

    ERROR:   invalid input syntax for type json (22P02)
    DETAIL:  Token "asd" is invalid.
    CONTEXT: line 5, column 8:

          3 | 'def',
          4 | 123,
          5 | 'foo', 'asd'::jsonb
                     ^

The format is based on what psql prints on errors (though not exactly
identical).

Based on the code I wrote for toml a few years ago:
https://github.com/BurntSushi/toml/blob/master/error.go

The regular error message now also includes the error code:

	pq: syntax error at end of input (42601)

psql doesn't print them, but I've found it useful on a number of
occasion, and I think in the context of a library like this it probably
makes sense.

Fixes #194
Fixes #521
Fixes #679
Fixes #895
@arp242 arp242 closed this in 4addd1f Jan 3, 2026
@arp242
Copy link
Copy Markdown
Collaborator

arp242 commented Jan 3, 2026

Fixed via #1219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Detail to Error() Report position of syntax errors in queries

2 participants