mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-06-22 22:37:41 +02:00
Better stacktrace in errors
This commit is contained in:
31
errors.go
31
errors.go
@ -11,12 +11,26 @@ type imgproxyError struct {
|
|||||||
Message string
|
Message string
|
||||||
PublicMessage string
|
PublicMessage string
|
||||||
Unexpected bool
|
Unexpected bool
|
||||||
|
|
||||||
|
stack []uintptr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *imgproxyError) Error() string {
|
func (e *imgproxyError) Error() string {
|
||||||
return e.Message
|
return e.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *imgproxyError) ErrorWithStack() string {
|
||||||
|
if e.stack == nil {
|
||||||
|
return e.Message
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s\n%s", e.Message, formatStack(e.stack))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *imgproxyError) StackTrace() []uintptr {
|
||||||
|
return e.stack
|
||||||
|
}
|
||||||
|
|
||||||
func newError(status int, msg string, pub string) *imgproxyError {
|
func newError(status int, msg string, pub string) *imgproxyError {
|
||||||
return &imgproxyError{
|
return &imgproxyError{
|
||||||
StatusCode: status,
|
StatusCode: status,
|
||||||
@ -28,18 +42,23 @@ func newError(status int, msg string, pub string) *imgproxyError {
|
|||||||
func newUnexpectedError(msg string, skip int) *imgproxyError {
|
func newUnexpectedError(msg string, skip int) *imgproxyError {
|
||||||
return &imgproxyError{
|
return &imgproxyError{
|
||||||
StatusCode: 500,
|
StatusCode: 500,
|
||||||
Message: fmt.Sprintf("Unexpected error: %s\n%s", msg, stacktrace(skip+3)),
|
Message: msg,
|
||||||
PublicMessage: "Internal error",
|
PublicMessage: "Internal error",
|
||||||
Unexpected: true,
|
Unexpected: true,
|
||||||
|
|
||||||
|
stack: callers(skip + 3),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func stacktrace(skip int) string {
|
func callers(skip int) []uintptr {
|
||||||
callers := make([]uintptr, 10)
|
stack := make([]uintptr, 10)
|
||||||
n := runtime.Callers(skip, callers)
|
n := runtime.Callers(skip, stack)
|
||||||
|
return stack[:n]
|
||||||
|
}
|
||||||
|
|
||||||
lines := make([]string, n)
|
func formatStack(stack []uintptr) string {
|
||||||
for i, pc := range callers[:n] {
|
lines := make([]string, len(stack))
|
||||||
|
for i, pc := range stack {
|
||||||
f := runtime.FuncForPC(pc)
|
f := runtime.FuncForPC(pc)
|
||||||
file, line := f.FileLine(pc)
|
file, line := f.FileLine(pc)
|
||||||
lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
|
lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
|
||||||
|
@ -109,7 +109,7 @@ func handlePanic(reqID string, rw http.ResponseWriter, r *http.Request, err erro
|
|||||||
reportError(err, r)
|
reportError(err, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
logResponse(reqID, ierr.StatusCode, ierr.Message)
|
logResponse(reqID, ierr.StatusCode, ierr.ErrorWithStack())
|
||||||
|
|
||||||
rw.WriteHeader(ierr.StatusCode)
|
rw.WriteHeader(ierr.StatusCode)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user