errors.go 839 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package main
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strings"
  6. )
  7. type imgproxyError struct {
  8. StatusCode int
  9. Message string
  10. PublicMessage string
  11. }
  12. func (e *imgproxyError) Error() string {
  13. return e.Message
  14. }
  15. func newError(status int, msg string, pub string) *imgproxyError {
  16. return &imgproxyError{status, msg, pub}
  17. }
  18. func newUnexpectedError(err error, skip int) *imgproxyError {
  19. msg := fmt.Sprintf("Unexpected error: %s\n%s", err, stacktrace(skip+1))
  20. return &imgproxyError{500, msg, "Internal error"}
  21. }
  22. func stacktrace(skip int) string {
  23. callers := make([]uintptr, 10)
  24. n := runtime.Callers(skip+1, callers)
  25. lines := make([]string, n)
  26. for i, pc := range callers[:n] {
  27. f := runtime.FuncForPC(pc)
  28. file, line := f.FileLine(pc)
  29. lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
  30. }
  31. return strings.Join(lines, "\n")
  32. }