forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
73 lines (59 loc) · 1.27 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"runtime"
"strings"
)
type imgproxyError struct {
StatusCode int
Message string
PublicMessage string
Unexpected bool
stack []uintptr
}
func (e *imgproxyError) Error() string {
return e.Message
}
func (e *imgproxyError) FormatStack() string {
if e.stack == nil {
return ""
}
return formatStack(e.stack)
}
func (e *imgproxyError) StackTrace() []uintptr {
return e.stack
}
func (e *imgproxyError) SetUnexpected(u bool) *imgproxyError {
e.Unexpected = u
return e
}
func newError(status int, msg string, pub string) *imgproxyError {
return &imgproxyError{
StatusCode: status,
Message: msg,
PublicMessage: pub,
}
}
func newUnexpectedError(msg string, skip int) *imgproxyError {
return &imgproxyError{
StatusCode: 500,
Message: msg,
PublicMessage: "Internal error",
Unexpected: true,
stack: callers(skip + 3),
}
}
func callers(skip int) []uintptr {
stack := make([]uintptr, 10)
n := runtime.Callers(skip, stack)
return stack[:n]
}
func formatStack(stack []uintptr) string {
lines := make([]string, len(stack))
for i, pc := range stack {
f := runtime.FuncForPC(pc)
file, line := f.FileLine(pc)
lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
}
return strings.Join(lines, "\n")
}