1

I'm trying to handle Postgres error. If user sends invalid uuid - Postgres returns an error with message: pq: invalid input syntax for type uuid:...

So, i want to check that error and if the error equals to the invalid input syntax error - i would like to show to the client 404 error, rather than returning server error.

But i can't find the defined error type for the returned error in Postgres library...

Can someone suggest me, where is the error returned?

2
  • Could you add more context? Are you using github.com/lib/pq? Commented Sep 23, 2021 at 19:46
  • 1
    A suggestion: you could validate the parameter before querying the database. Commented Sep 23, 2021 at 19:48

1 Answer 1

2

In the query you can cast the uuid column to text. Here are two ways to do that:

SELECT * FROM user WHERE id::text = 'sometext'
SELECT * FROM user WHERE text(id) = 'sometext'

Also discussed here: https://stackoverflow.com/a/46494463/2316115

PostgreSQL Type Casts are documented here: https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.