小编Cra*_*les的帖子

使用Rails和Postgres时如何访问数据库错误信息

我使用find_by_sql连接到Postgres数据库并执行数据库功能.数据库函数执行许多SQL语句并根据需要引发异常.

如何捕获Rails中Postgres函数引发的错误代码和错误消息?

def self.validate_email(systemuserid, emailaddress)
  begin
    result = (self.find_by_sql(["SELECT fn_systemuser_validate_email(?, ?) AS returncode", 
                                systemuserid, emailaddress])).first
  rescue => err
    # I want to get access to the error code and error message here and act accordingly
    # errno = ??
    # errmsg = ??
    if errno == 10000
    end
  end
  return result[:returncode]
end
Run Code Online (Sandbox Code Playgroud)

我开始试图在连接对象中找到这些信息 - 没有这样的运气.

任何帮助非常感谢.

postgresql error-handling model ruby-on-rails

5
推荐指数
1
解决办法
2074
查看次数

标签 统计

error-handling ×1

model ×1

postgresql ×1

ruby-on-rails ×1