-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathhandler_error.go
59 lines (52 loc) · 1.25 KB
/
handler_error.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"github.com/gorilla/context"
"net/http"
"runtime/pprof"
"strings"
"time"
)
type ErrorHandler struct {
TykMiddleware
}
func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err string, err_code int) {
if config.EnableAnalytics {
t := time.Now()
keyName := r.Header.Get(e.Spec.APIDefinition.Auth.AuthHeaderName)
version := e.Spec.getVersionFromRequest(r)
if version == "" {
version = "Non Versioned"
}
if e.TykMiddleware.Spec.APIDefinition.Proxy.StripListenPath {
r.URL.Path = strings.Replace(r.URL.Path, e.TykMiddleware.Spec.Proxy.ListenPath, "", 1)
}
thisRecord := AnalyticsRecord{
r.Method,
r.URL.Path,
r.ContentLength,
r.Header.Get("User-Agent"),
t.Day(),
t.Month(),
t.Year(),
t.Hour(),
err_code,
keyName,
t,
version,
e.Spec.APIDefinition.Name,
e.Spec.APIDefinition.APIID,
e.Spec.APIDefinition.OrgID}
analytics.RecordHit(thisRecord)
}
w.WriteHeader(err_code)
w.Header().Add("Content-Type", "application/json")
w.Header().Add("X-Generator", "tyk.io")
thisError := ApiError{fmt.Sprintf("%s", err)}
templates.ExecuteTemplate(w, "error.json", &thisError)
if doMemoryProfile {
pprof.WriteHeapProfile(prof_file)
}
// Clean up
context.Clear(r)
}