forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
45 lines (36 loc) · 965 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
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"log"
"runtime"
"strings"
)
type imgproxyError struct {
StatusCode int
Message string
PublicMessage string
}
func (e *imgproxyError) Error() string {
return e.Message
}
func newError(status int, msg string, pub string) *imgproxyError {
return &imgproxyError{status, msg, pub}
}
func newUnexpectedError(err error, skip int) *imgproxyError {
msg := fmt.Sprintf("Unexpected error: %s\n%s", err, stacktrace(skip+1))
return &imgproxyError{500, msg, "Internal error"}
}
func stacktrace(skip int) string {
callers := make([]uintptr, 10)
n := runtime.Callers(skip+1, callers)
lines := make([]string, n)
for i, pc := range callers[:n] {
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")
}
func warning(f string, args ...interface{}) {
log.Printf("\033[1;33m[WARNING]\033[0m %s", fmt.Sprintf(f, args...))
}