golang pq sql驱动程序:pq:类型uuid的输入语法无效错误类型

huangapple go评论104阅读模式
英文:

golang pq sql driver: pq: invalid input syntax for type uuid error type

问题

我正在尝试处理Postgres错误。
如果用户发送了无效的uuid - Postgres会返回一个带有消息的错误:pq: invalid input syntax for type uuid:...

所以,我想检查这个错误,如果错误等于无效的输入语法错误 - 我想显示给客户端404错误,而不是返回服务器错误。

但是我找不到Postgres库中返回的错误的定义错误类型...

有人能告诉我错误是在哪里返回的吗?

英文:

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?

答案1

得分: 1

在查询中,你可以将 uuid 列转换为 text 类型。以下是两种方法:

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

这里也有相关讨论:
https://stackoverflow.com/a/46494463/2316115

PostgreSQL 类型转换的文档在这里:
https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS

英文:

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

huangapple
  • 本文由 发表于 2021年9月24日 03:19:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/69305845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定