-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy patherrors.go
36 lines (31 loc) · 1.04 KB
/
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
36
// 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"
"net/http"
log "github.com/sirupsen/logrus"
)
// ErrAuthNotFound is for when autographer.getAuthByID doesn't find an auth
var ErrAuthNotFound = fmt.Errorf("authorization not found")
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(io.Discard, r.Body)
r.Body.Close()
}
http.Error(w, msg, errorCode)
}