1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/errors.go

74 lines
1.3 KiB
Go
Raw Normal View History

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