errors.go 836 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(msg string, skip int) *imgproxyError {
  19. return &imgproxyError{
  20. 500,
  21. fmt.Sprintf("Unexpected error: %s\n%s", msg, stacktrace(skip+3)),
  22. "Internal error",
  23. }
  24. }
  25. func stacktrace(skip int) string {
  26. callers := make([]uintptr, 10)
  27. n := runtime.Callers(skip, callers)
  28. lines := make([]string, n)
  29. for i, pc := range callers[:n] {
  30. f := runtime.FuncForPC(pc)
  31. file, line := f.FileLine(pc)
  32. lines[i] = fmt.Sprintf("%s:%d %s", file, line, f.Name())
  33. }
  34. return strings.Join(lines, "\n")
  35. }