go - Golang error handling mistake -



go - Golang error handling mistake -

the next programme custom version of webserver official tutorial @ this page. i've modified save() , loadpage() functions save pages neo4j using jmcvetta's bundle neoism.

// web1 project main.go bundle main import ( "errors" "fmt" "github.com/jmcvetta/neoism" "html/template" "io/ioutil" "net/http" "regexp" ) type page struct { title string body string } func (p *page) save() error { db, err := neoism.connect("http://localhost:7474/db/data") if err != nil { homecoming err } res := []struct { n neoism.node }{} cq := neoism.cypherquery{ statement: "merge (n:page {title: {title}}) on match set n.body = {body} homecoming n", parameters: neoism.props{"title": p.title, "body": p.body}, result: &res, } db.cypher(&cq) homecoming nil } func loadpage(title string) (*page, error) { db, err := neoism.connect("http://localhost:7474/db/data") if err != nil { homecoming nil, err } res := []struct { title string `json:"a.title"` // `json` tag matches column name in query body string `json:"a.body"` }{} cq := neoism.cypherquery{ statement: ` match (a:page) a.title = {name} homecoming a.title, a.body `, parameters: neoism.props{"name": title}, result: &res, } db.cypher(&cq) r := res[0] if r.title == "" || r.body == "" { homecoming nil, errors.new("page doesn't exist") } homecoming &page{title: r.title, body: r.body}, nil } func viewhandler(w http.responsewriter, r *http.request, title string) { p, err := loadpage(title) if err != nil { http.redirect(w, r, "/edit/"+title, http.statusfound) homecoming } rendertemplate(w, "view", p) } func edithandler(w http.responsewriter, r *http.request, title string) { p, err := loadpage(title) if err != nil { p = &page{title: title} } rendertemplate(w, "edit", p) } func savehandler(w http.responsewriter, r *http.request, title string) { body := r.formvalue("body") p := &page{title: title, body: body} err := p.save() if err != nil { http.error(w, err.error(), http.statusinternalservererror) homecoming } http.redirect(w, r, "/view/"+title, http.statusfound) } func homehandler(w http.responsewriter, r *http.request) { body, err := ioutil.readfile("home.html") if err != nil { http.error(w, err.error(), http.statusinternalservererror) homecoming } fmt.fprintf(w, "%s", body) } func roothandler(w http.responsewriter, r *http.request) { http.redirect(w, r, "/home", http.statusfound) } var templates = template.must(template.parsefiles("edit.html", "view.html")) func rendertemplate(w http.responsewriter, tmpl string, p *page) { err := templates.executetemplate(w, tmpl+".html", p) if err != nil { http.error(w, err.error(), http.statusinternalservererror) } } var validpath = regexp.mustcompile("^/(edit|save|view)/([a-za-z0-9]+)$") func makehandler(fn func(http.responsewriter, *http.request, string)) http.handlerfunc { homecoming func(w http.responsewriter, r *http.request) { m := validpath.findstringsubmatch(r.url.path) if m == nil { http.notfound(w, r) homecoming } fn(w, r, m[2]) } } func main() { http.handlefunc("/view/", makehandler(viewhandler)) http.handlefunc("/edit/", makehandler(edithandler)) http.handlefunc("/save/", makehandler(savehandler)) http.handlefunc("/home", homehandler) http.handlefunc("/", roothandler) http.listenandserve(":8082", nil) }

my problem is, believe, in the loadpage() function, doesn't seem homecoming error when page returned has empty body, causing handlers seek executing templates empty structs (at to the lowest degree tend think that's is). 1 see did wrong ?

edit : output when seek load page doesn't exist :

2014/06/19 23:13:35 http: panic serving 127.0.0.1:60326: runtime error: index out of range goroutine 5 [running]: net/http.func·009() c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1093 +0xb1 runtime.panic(0x6d1f20, 0xa977d7) c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/runtime/panic.c:248 +0x11b main.loadpage(0xc08407a12a, 0x6, 0x332978, 0xaa7560, 0x3) c:/users/nicolas/go/src/web1/main.go:55 +0x369 main.edithandler(0x332940, 0xc08400b140, 0xc084019750, 0xc08407a12a, 0x6) c:/users/nicolas/go/src/web1/main.go:72 +0x34 main.func·001(0x332940, 0xc08400b140, 0xc084019750) c:/users/nicolas/go/src/web1/main.go:121 +0xc9 net/http.handlerfunc.servehttp(0xc084024ae0, 0x332940, 0xc08400b140, 0xc084019750) c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1220 +0x43 net/http.(*servemux).servehttp(0xc0840381b0, 0x332940, 0xc08400b140, 0xc084019750) c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1496 +0x166 net/http.serverhandler.servehttp(0xc084005e10, 0x332940, 0xc08400b140, 0xc084019750) c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1597 +0x171 net/http.(*conn).serve(0xc08403f580) c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1167 +0x7ba created net/http.(*server).serve c:/users/admini~1/appdata/local/temp/2/makerelease250988475/go/src/pkg/net/http/server.go:1644 +0x28e

precision : pages exist, programme works intended, i'm able edit pages , load them later.

without running code suspect error here:

db.cypher(&cq) r := res[0] if r.title == "" || r.body == "" { homecoming nil, errors.new("page doesn't exist") }

the code assumes there res[0] value. should check

if len(res) == 0 { homecoming nil, errors.new("page not exists") }

you error in code in loadpage, , index error.

error-handling go neo4j

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -