error2.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // This file contains the code snippets included in "Error Handling and Go."
  5. package main
  6. import (
  7. "net/http"
  8. "text/template"
  9. )
  10. func init() {
  11. http.HandleFunc("/view", viewRecord)
  12. }
  13. func viewRecord(w http.ResponseWriter, r *http.Request) {
  14. c := appengine.NewContext(r)
  15. key := datastore.NewKey(c, "Record", r.FormValue("id"), 0, nil)
  16. record := new(Record)
  17. if err := datastore.Get(c, key, record); err != nil {
  18. http.Error(w, err.Error(), http.StatusInternalServerError)
  19. return
  20. }
  21. if err := viewTemplate.Execute(w, record); err != nil {
  22. http.Error(w, err.Error(), http.StatusInternalServerError)
  23. }
  24. }
  25. // STOP OMIT
  26. type ap struct{}
  27. func (ap) NewContext(*http.Request) *ctx { return nil }
  28. type ctx struct{}
  29. func (*ctx) Errorf(string, ...interface{}) {}
  30. var appengine ap
  31. type ds struct{}
  32. func (ds) NewKey(*ctx, string, string, int, *int) string { return "" }
  33. func (ds) Get(*ctx, string, *Record) error { return nil }
  34. var datastore ds
  35. type Record struct{}
  36. var viewTemplate *template.Template
  37. func main() {}