comparison pkg/middleware/jsonhandler.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents f4ec3558460e
children 6270951dda28
comparison
equal deleted inserted replaced
5488:a726a92ea5c9 5490:5f47eeea988d
17 "context" 17 "context"
18 "database/sql" 18 "database/sql"
19 "encoding/json" 19 "encoding/json"
20 "fmt" 20 "fmt"
21 "io" 21 "io"
22 "log"
23 "net/http" 22 "net/http"
24 23
25 "github.com/jackc/pgx" 24 "github.com/jackc/pgx"
26 25
27 "gemma.intevation.de/gemma/pkg/auth" 26 "gemma.intevation.de/gemma/pkg/auth"
27 "gemma.intevation.de/gemma/pkg/log"
28 ) 28 )
29 29
30 // JSONResult defines the return type of JSONHandler handler function. 30 // JSONResult defines the return type of JSONHandler handler function.
31 type JSONResult struct { 31 type JSONResult struct {
32 // Code is the HTTP status code to be set which defaults to http.StatusOK (200). 32 // Code is the HTTP status code to be set which defaults to http.StatusOK (200).
135 } else { 135 } else {
136 jr, err = j.Handle(req) 136 jr, err = j.Handle(req)
137 } 137 }
138 138
139 if err != nil { 139 if err != nil {
140 log.Printf("error: %v\n", err) 140 log.Errorf("%v\n", err)
141 switch e := err.(type) { 141 switch e := err.(type) {
142 case pgx.PgError: 142 case pgx.PgError:
143 var res = struct { 143 var res = struct {
144 Result string `json:"result"` 144 Result string `json:"result"`
145 Code string `json:"code"` 145 Code string `json:"code"`
150 Message: e.Message, 150 Message: e.Message,
151 } 151 }
152 rw.Header().Set("Content-Type", "application/json") 152 rw.Header().Set("Content-Type", "application/json")
153 rw.WriteHeader(http.StatusInternalServerError) 153 rw.WriteHeader(http.StatusInternalServerError)
154 if err := json.NewEncoder(rw).Encode(&res); err != nil { 154 if err := json.NewEncoder(rw).Encode(&res); err != nil {
155 log.Printf("error: %v\n", err) 155 log.Errorf("%v\n", err)
156 } 156 }
157 case JSONError: 157 case JSONError:
158 rw.Header().Set("Content-Type", "application/json") 158 rw.Header().Set("Content-Type", "application/json")
159 if e.Code == 0 { 159 if e.Code == 0 {
160 e.Code = http.StatusInternalServerError 160 e.Code = http.StatusInternalServerError
164 Message string `json:"message"` 164 Message string `json:"message"`
165 }{ 165 }{
166 Message: e.Message, 166 Message: e.Message,
167 } 167 }
168 if err := json.NewEncoder(rw).Encode(&res); err != nil { 168 if err := json.NewEncoder(rw).Encode(&res); err != nil {
169 log.Printf("error: %v\n", err) 169 log.Errorf("%v\n", err)
170 } 170 }
171 default: 171 default:
172 http.Error(rw, 172 http.Error(rw,
173 "error: "+err.Error(), 173 "error: "+err.Error(),
174 http.StatusInternalServerError) 174 http.StatusInternalServerError)
192 _, err = io.Copy(rw, r) 192 _, err = io.Copy(rw, r)
193 } else { 193 } else {
194 err = json.NewEncoder(rw).Encode(jr.Result) 194 err = json.NewEncoder(rw).Encode(jr.Result)
195 } 195 }
196 if err != nil { 196 if err != nil {
197 log.Printf("error: %v\n", err) 197 log.Errorf("%v\n", err)
198 } 198 }
199 } 199 }
200 } 200 }
201 201
202 // SendJSON sends data JSON encoded to the response writer 202 // SendJSON sends data JSON encoded to the response writer
204 func SendJSON(rw http.ResponseWriter, code int, data interface{}) { 204 func SendJSON(rw http.ResponseWriter, code int, data interface{}) {
205 rw.Header().Set("Content-Type", "application/json") 205 rw.Header().Set("Content-Type", "application/json")
206 rw.Header().Set("X-Content-Type-Options", "nosniff") 206 rw.Header().Set("X-Content-Type-Options", "nosniff")
207 rw.WriteHeader(code) 207 rw.WriteHeader(code)
208 if err := json.NewEncoder(rw).Encode(data); err != nil { 208 if err := json.NewEncoder(rw).Encode(data); err != nil {
209 log.Printf("error: %v\n", err) 209 log.Errorf("%v\n", err)
210 } 210 }
211 } 211 }