forked from mozilla-services/autograph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
35 lines (31 loc) · 953 Bytes
/
errors.go
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: Julien Vehent [email protected] [:ulfr]
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
func httpError(w http.ResponseWriter, r *http.Request, errorCode int, errorMessage string, args ...interface{}) {
rid := getRequestID(r)
log.WithFields(log.Fields{
"code": errorCode,
"rid": rid,
}).Errorf(errorMessage, args...)
msg := fmt.Sprintf(errorMessage, args...)
msg += "\r\nrequest-id: " + rid
// when nginx is in front of go, nginx requires that the entire
// request body is read before writing a response.
// https://github.com/golang/go/issues/15789
if r.Body != nil {
io.Copy(ioutil.Discard, r.Body)
r.Body.Close()
}
http.Error(w, msg, errorCode)
return
}