| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package backend | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "net/http" | |
| 10 | |
| 11 log "github.com/luci/luci-go/common/logging" | |
| 12 "golang.org/x/net/context" | |
| 13 ) | |
| 14 | |
| 15 // httpError | |
| 16 type httpError struct { | |
| 17 reason error | |
| 18 code int | |
| 19 } | |
| 20 | |
| 21 func (e *httpError) Error() string { | |
| 22 if r := e.reason; r != nil { | |
| 23 return fmt.Sprintf("%v: %q", e.reason, http.StatusText(e.code)) | |
| 24 } | |
| 25 return http.StatusText(e.code) | |
| 26 } | |
| 27 | |
| 28 func newHTTPError(reason error, code int) error { | |
| 29 return &httpError{reason, code} | |
| 30 } | |
| 31 | |
| 32 // errorWrapper wraps an error-returning function and responds with an | |
| 33 // InternalServerError if an error is returned. | |
| 34 func errorWrapper(c context.Context, w http.ResponseWriter, f func() error) { | |
| 35 if err := f(); err != nil { | |
| 36 statusCode := http.StatusInternalServerError | |
| 37 if e, ok := err.(*httpError); ok { | |
| 38 statusCode = e.code | |
| 39 } | |
| 40 | |
| 41 log.Fields{ | |
| 42 log.ErrorKey: err, | |
| 43 "statusCode": statusCode, | |
| 44 }.Errorf(c, "Backend handler returned error.") | |
| 45 w.WriteHeader(statusCode) | |
| 46 } | |
| 47 } | |
| OLD | NEW |