golang appengine内部服务器错误

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

golang appengine internal server error

问题

我正在尝试在GAE上使用Go开发自定义的用户模型/身份验证代码。以下代码是对demos/guestbook应用程序中的一些代码进行简单修改的结果:

q := datastore.NewQuery("User").Filter("Email =", email)
users := make([]User, 0, 1)
if _, err := q.GetAll(c, &users); err != nil {
   http.Error(w, err.Error(), http.StatusInternalServerError)
   return
}

其中email是从表单中获取的。它只是查询数据存储中具有给定电子邮件的用户。如果具有该电子邮件的用户存在,它运行良好;如果不存在,则会显示“内部服务器错误”页面。我不明白的是(我猜是关于Go中的错误处理,或者可能是数据存储查询),为什么我不能在if error块中执行其他操作。像这样稍微修改一下:

if _, err := q.GetAll(c, &users); err != nil {
   fmt.Fprintf(w, "%s\n", "user not found")
}

会产生相同的“内部服务器错误”页面,而不是只打印“user not found”。

谢谢!

英文:

I am trying to develop custom User model/authentication code in Go on GAE. The following code is a simple modification of some code in the demos/guestbook application:

q := datastore.NewQuery("User").Filter("Email =", email)
users := make([]User, 0, 1)
if _, err := q.GetAll(c, &users); err != nil {
   http.Error(w, err.Error(), http.StatusInternalServerError)
   return
}

Where email is pulled from a form. It just queries the datastore for a User with the given email. It works fine if the User with the email exists, and dies with an "Internal Server Error" page if they don't. What I don't understand (I guess about error handling in Go, or maybe datastore querying), is why I can't do anything else within that if error block. A slight mod like this:

if _, err := q.GetAll(c, &users); err != nil {
   fmt.Fprintf(w, "%s\n", "user not found")
}

produces the same "Internal Server Error" page instead of just printing "user not found".

Thanks!

答案1

得分: 2

没有看到你的代码的其他部分很难说,但主要问题是当你这样写的时候:

if _, err := q.GetAll(c, &users); err != nil {
   fmt.Fprintf(w, "%s\n", "user not found")
}

内部语句只有在查询期间出现错误时才会触发,而不是在查询返回空结果时触发。因此,你实际上没有进入那段代码 - 我最好的猜测(没有看到代码)是你的Internal Server Error在其他地方被触发,也许是在你将users作为包含数据的变量处理的地方。如果你想在没有匹配到用户时打印出该消息,你可以简单地检查响应的长度 - 如果为0,表示没有返回结果,你可以打印出你的消息:

     if len(users) == 0 {
         fmt.Fprintf(w, "%s\n", "user not found")
     }

可能还有更符合惯用方式的方法,但我相信这对你的情况可以工作(它将按照你在那里所述打印 - 你可能希望以不同的方式处理它)。

英文:

It is tough to say without seeing the rest of your code, but the main point is that when you say:

if _, err := q.GetAll(c, &users); err != nil {
   fmt.Fprintf(w, "%s\n", "user not found")
}

The inner statement will only be triggered if there was an error during the query, not if the query returns nothing. Therefore you aren't actually entering that block of code - my best guess (without seeing the code) is that your Internal Server Error is being triggered elsewhere, perhaps somewhere where you are treating users as a variable that contains data. If you want to print out that message if no user was matched, you could do something simple like check the length of the response - if it is 0, no results were returned and you can print your message:

     if len(users) == 0 {
         fmt.Fprintf(w, "%s\n", "user not found")
     }

There is likely a more idiomatic way, but I believe that will work for your situation (it will print just as you state there - you may want to handle it differently).

huangapple
  • 本文由 发表于 2013年1月9日 08:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/14226629.html
匿名

发表评论

匿名网友

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

确定