Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for empty argument to :csv ? #442

Closed
njlr opened this issue Jan 8, 2018 · 5 comments
Closed

Improve error message for empty argument to :csv ? #442

njlr opened this issue Jan 8, 2018 · 5 comments

Comments

@njlr
Copy link

njlr commented Jan 8, 2018

I have a query like this:

const fetchBooks = (db, titles) => db.query(`SELECT id
    FROM books 
    WHERE title IN ( $<titles:csv> ) `, {
      titles,
    });

When titles is [], the error message is quite confusing:

... error: syntax error at or near ")" ...

This is the error forwarded from the underlying pg library, but perhaps pg-promise could provide a better error for this scenario?

Something like...

Empty arrays are not valid for the ":csv" syntax.

(Related: brianc/node-postgres#1079)

@vitaly-t
Copy link
Owner

vitaly-t commented Jan 8, 2018

This is the error forwarded from the underlying pg library, but perhaps pg-promise could provide a better error for this scenario?

It can't, because filter :csv is generic, and it doesn't know in which context you are using it.

You should instead check titles.length, and when it is 0, do not execute such invalid query at all, i.e. having IN () is invalid in queries, and that's what you are trying to execute, hence the error.

@vitaly-t vitaly-t closed this as completed Jan 8, 2018
@njlr
Copy link
Author

njlr commented Jan 8, 2018

Yes, that was my work-around. It would have been easier to identify the error with a more specific message though.

If there are cases in which :csv is valid without any elements then my suggestion will not work.

@vitaly-t
Copy link
Owner

vitaly-t commented Jan 8, 2018

If there are cases in which :csv is valid without any elements then my suggestion will not work.

There are plenty, as the filter is generic, it is used in many places, not just for IN () statements.

@vitaly-t
Copy link
Owner

vitaly-t commented Jan 19, 2018

@vitaly-t
Copy link
Owner

vitaly-t commented Mar 9, 2018

More on this question: WHERE col IN Query with empty array as parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants