相关疑难解决方法(0)

更好的错误处理

在这里https://github.com/astaxie/build-web-application-with-golang/blob/master/en/11.1.md描述了如何根据http包增强自定义路由器和自定义错误类型的错误处理。

type appError struct {
    Error   error
    Message string
    Code    int
}    

type appHandler func(http.ResponseWriter, *http.Request) *appError
// custom handler catching errors
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if e := fn(w, r); e != nil { // e is *appError, not os.Error.
        c := appengine.NewContext(r)
        c.Errorf("%v", e.Error)
        http.Error(w, e.Message, e.Code)
    }
}
// fetch data or return *appError
func viewRecord(w http.ResponseWriter, r *http.Request) *appError {
    c := appengine.NewContext(r)
    key := datastore.NewKey(c, "Record", r.FormValue("id"), 0, nil) …
Run Code Online (Sandbox Code Playgroud)

error-handling web-applications http go

3
推荐指数
1
解决办法
3692
查看次数

标签 统计

error-handling ×1

go ×1

http ×1

web-applications ×1