From fc49c5701fde7d0c9dd2fa321ba0ebbff712b08a Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Mon, 20 May 2019 03:32:14 -0400 Subject: streamlined error handling and misc minor fixes --- query.go | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'query.go') diff --git a/query.go b/query.go index 63deb55..c47cfb0 100644 --- a/query.go +++ b/query.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "net/http" "strings" @@ -11,23 +12,33 @@ import ( func apiErrCheck(err error, r *http.Request) { if err != nil { uip := getIPFromCtx(r.Context()) - log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err) + log.Printf("*** %v :: %v %v :: %v\n", uip, r.Method, r.URL, err) } } -func apiErrCheck500(err error, w http.ResponseWriter, r *http.Request) { - if err != nil { - uip := getIPFromCtx(r.Context()) - log.Printf("%v :: %v %v :: %v\n", uip, r.Method, r.URL, err) - http.Error(w, err.Error(), http.StatusInternalServerError) +// Takes the output of queries and formats it for +// an HTTP response. Iterates over the string slice, +// appending each entry to a byte slice, and adding +// newlines where appropriate. +func parseQueryOut(out []string) []byte { + var data []byte + + for _, e := range out { + data = append(data, []byte(e)...) + + if !strings.HasSuffix(e, "\n") { + data = append(data, byte('\n')) + } } + + return data } // apiUserQuery is called via apiEndpointHandler when // the endpoint is "users" and r.FormValue("q") is not empty. // It queries the registry cache for users or user URLs // matching the term supplied via r.FormValue("q") -func apiEndpointQuery(w http.ResponseWriter, r *http.Request) { +func apiEndpointQuery(w http.ResponseWriter, r *http.Request) error { query := r.FormValue("q") urls := r.FormValue("url") var out []string @@ -64,20 +75,16 @@ func apiEndpointQuery(w http.ResponseWriter, r *http.Request) { apiErrCheck(err, r) default: - http.Error(w, "500", http.StatusInternalServerError) + return fmt.Errorf("endpoint query, no cases match") } - // iterate over the output. if there aren't - // explicit newlines, add them. - var data []byte - for _, e := range out { - data = append(data, []byte(e)...) - if !strings.HasSuffix(e, "\n") { - data = append(data, byte('\n')) - } - } + data := parseQueryOut(out) w.Header().Set("Content-Type", txtutf8) _, err = w.Write(data) - apiErrCheck500(err, w, r) + if err != nil { + return err + } + + return nil } -- cgit 1.4.1-2-gfad0 value='range'>range
path: root/charterm/planet-docs/doc/scribble-common.js
blob: 00eec767fc23f7acfbb7603ad803600a7d13dce1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153