2017-10-04 21:44:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type imgproxyError struct {
|
|
|
|
StatusCode int
|
|
|
|
Message string
|
|
|
|
PublicMessage string
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:53:44 +02:00
|
|
|
func (e *imgproxyError) Error() string {
|
2017-10-04 21:44:58 +02:00
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:53:44 +02:00
|
|
|
func newError(status int, msg string, pub string) *imgproxyError {
|
|
|
|
return &imgproxyError{status, msg, pub}
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 16:37:26 +02:00
|
|
|
func newUnexpectedError(msg string, skip int) *imgproxyError {
|
|
|
|
return &imgproxyError{
|
|
|
|
500,
|
|
|
|
fmt.Sprintf("Unexpected error: %s\n%s", msg, stacktrace(skip+3)),
|
|
|
|
"Internal error",
|
|
|
|
}
|
2017-10-04 21:44:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stacktrace(skip int) string {
|
|
|
|
callers := make([]uintptr, 10)
|
2019-05-08 16:37:26 +02:00
|
|
|
n := runtime.Callers(skip, callers)
|
2017-10-04 21:44:58 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|