blob: 2b0e0c3563a56d4738ec6deadcb850e2490624a1 [file] [log] [blame]
Andrew Gerrandc400a0b2011-12-13 09:44:06 +11001// 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
5// This file contains the code snippets included in "Error Handling and Go."
6
7package main
8
9import (
10 "net/http"
11 "text/template"
12)
13
14func init() {
15 http.HandleFunc("/view", viewRecord)
16}
17
18func viewRecord(w http.ResponseWriter, r *http.Request) {
19 c := appengine.NewContext(r)
20 key := datastore.NewKey(c, "Record", r.FormValue("id"), 0, nil)
21 record := new(Record)
22 if err := datastore.Get(c, key, record); err != nil {
23 http.Error(w, err.Error(), 500)
24 return
25 }
26 if err := viewTemplate.Execute(w, record); err != nil {
27 http.Error(w, err.Error(), 500)
28 }
29}
Andrew Gerrand468e6922012-01-09 20:05:34 +110030
Andrew Gerrandc400a0b2011-12-13 09:44:06 +110031// STOP OMIT
32
33type ap struct{}
34
35func (ap) NewContext(*http.Request) *ctx { return nil }
36
37type ctx struct{}
38
39func (*ctx) Errorf(string, ...interface{}) {}
40
41var appengine ap
42
43type ds struct{}
44
45func (ds) NewKey(*ctx, string, string, int, *int) string { return "" }
46func (ds) Get(*ctx, string, *Record) error { return nil }
47
48var datastore ds
49
50type Record struct{}
51
52var viewTemplate *template.Template
53
54func main() {}