errors.go 965 B

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