1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-01-23 11:14:48 +02:00
imgproxy/errors.go

74 lines
1.3 KiB
Go
Raw Normal View History

2017-10-05 01:44:58 +06:00
package main
import (
"fmt"
"runtime"
"strings"
)
type imgproxyError struct {
StatusCode int
Message string
PublicMessage string
2019-08-15 19:15:37 +06:00
Unexpected bool
2019-08-19 18:05:34 +06:00
stack []uintptr
2017-10-05 01:44:58 +06:00
}
2018-11-20 18:53:44 +06:00
func (e *imgproxyError) Error() string {
2017-10-05 01:44:58 +06:00
return e.Message
}
func (e *imgproxyError) FormatStack() string {
2019-08-19 18:05:34 +06:00
if e.stack == nil {
return ""
2019-08-19 18:05:34 +06:00
}
return formatStack(e.stack)
2019-08-19 18:05:34 +06:00
}
func (e *imgproxyError) StackTrace() []uintptr {
return e.stack
}
func (e *imgproxyError) SetUnexpected(u bool) *imgproxyError {
e.Unexpected = u
2019-08-20 20:23:26 +06:00
return e
}
2018-11-20 18:53:44 +06:00
func newError(status int, msg string, pub string) *imgproxyError {
2019-08-15 19:15:37 +06:00
return &imgproxyError{
StatusCode: status,
Message: msg,
PublicMessage: pub,
}
2017-10-05 01:44:58 +06:00
}
2019-05-08 20:37:26 +06:00
func newUnexpectedError(msg string, skip int) *imgproxyError {
return &imgproxyError{
2019-08-15 19:15:37 +06:00
StatusCode: 500,
2019-08-19 18:05:34 +06:00
Message: msg,
2019-08-15 19:15:37 +06:00
PublicMessage: "Internal error",
Unexpected: true,
2019-08-19 18:05:34 +06:00
stack: callers(skip + 3),
2019-05-08 20:37:26 +06:00
}
2017-10-05 01:44:58 +06:00
}
2019-08-19 18:05:34 +06:00
func callers(skip int) []uintptr {
stack := make([]uintptr, 10)
n := runtime.Callers(skip, stack)
return stack[:n]
}
2017-10-05 01:44:58 +06:00
2019-08-19 18:05:34 +06:00
func formatStack(stack []uintptr) string {
lines := make([]string, len(stack))
for i, pc := range stack {
2017-10-05 01:44:58 +06:00
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")
}