errors.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package module
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "runtime"
  9. )
  10. // Err... are errors used globally
  11. var (
  12. ErrUnknown = errors.New("Unknown error")
  13. // ErrRetrievingImage = errors.New("Error retrieving image")
  14. // ErrInvalidURL = errors.New("Passed URL is not valid")
  15. // ErrDecodingImage = errors.New("Error decoding image")
  16. // ErrWidthNoInteger = errors.New("Error parsing width parameter as integer")
  17. // ErrHeightNoInteger = errors.New("Error parsing height parameter as integer")
  18. // ErrAspectRatioWidthNoInteger = errors.New("Error parsing aspect ratio width parameter as integer")
  19. // ErrAspectRatioHeightNoInteger = errors.New("Error parsing aspect ratio height parameter as integer")
  20. ErrReadingRequestBody = errors.New("Error reading request body")
  21. ErrDecodingJSON = errors.New("Error decoding JSON")
  22. // ErrDecodingBase64 = errors.New("Error decoding base64 encoded image")
  23. // ErrReadingCroppedImage = errors.New("Error reading cropped image")
  24. // ErrCreatingThumbnail = errors.New("Error creating thumbnail")
  25. // ErrDecodingImageInternal = errors.New("Error decoding image")
  26. // ErrEncodingImage = errors.New("Error encoding image")
  27. ErrSendingResponse = errors.New("Error sending response")
  28. ErrEncodingJSON = errors.New("Error encoding JSON")
  29. ErrDatabase = errors.New("Database error")
  30. ErrRetrievingTestData = errors.New("Error receiving Testdata")
  31. ErrParsingInt = errors.New("Error parsing string to int")
  32. )
  33. // StatusCodeMapper maps errors to HTTP Status codes
  34. var StatusCodeMapper = map[error]int{
  35. ErrUnknown: http.StatusInternalServerError,
  36. // ErrRetrievingImage: http.StatusBadRequest,
  37. // ErrInvalidURL: http.StatusBadRequest,
  38. // ErrDecodingImage: http.StatusBadRequest,
  39. // ErrWidthNoInteger: http.StatusBadRequest,
  40. // ErrHeightNoInteger: http.StatusBadRequest,
  41. // ErrAspectRatioWidthNoInteger: http.StatusBadRequest,
  42. // ErrAspectRatioHeightNoInteger: http.StatusBadRequest,
  43. ErrReadingRequestBody: http.StatusBadRequest,
  44. ErrDecodingJSON: http.StatusBadRequest,
  45. // ErrDecodingBase64: http.StatusBadRequest,
  46. // ErrReadingCroppedImage: http.StatusInternalServerError,
  47. // ErrCreatingThumbnail: http.StatusInternalServerError,
  48. // ErrDecodingImageInternal: http.StatusInternalServerError,
  49. // ErrEncodingImage: http.StatusInternalServerError,
  50. ErrSendingResponse: http.StatusInternalServerError,
  51. ErrEncodingJSON: http.StatusInternalServerError,
  52. ErrDatabase: http.StatusInternalServerError,
  53. ErrRetrievingTestData: http.StatusBadGateway,
  54. ErrParsingInt: http.StatusInternalServerError,
  55. }
  56. // MetaError is two errors in one
  57. type MetaError struct {
  58. outerError error
  59. innerError error
  60. httpStatus int
  61. }
  62. // NewMetaError returns a new MetaError and automatically selects the suited HTTP Status Code
  63. func NewMetaError(outerError error, innerError error) MetaError {
  64. return MetaError{outerError, innerError, StatusCodeMapper[outerError]}
  65. }
  66. // NewMetaErrorWithStatus returns a new MetaError with manually selected HTTP Status Code
  67. func NewMetaErrorWithStatus(outerError error, innerError error, httpStatus int) MetaError {
  68. return MetaError{outerError, innerError, httpStatus}
  69. }
  70. func (err MetaError) Error() string {
  71. return err.outerError.Error() + ": " + err.innerError.Error()
  72. }
  73. type StatusError struct {
  74. error
  75. httpStatus int
  76. }
  77. func NewStatusError(err error, status int) StatusError {
  78. return StatusError{err, status}
  79. }
  80. // HandleError will handle errors
  81. func HandleError(outerErr error, innerErr error) {
  82. if innerErr != nil {
  83. switch outerErr {
  84. case ErrDatabase:
  85. switch innerErr.Error() {
  86. case "record not found":
  87. panic(NewStatusError(innerErr, http.StatusNotFound))
  88. }
  89. }
  90. panic(NewMetaError(outerErr, innerErr))
  91. }
  92. }
  93. // HTTPStatusHandler ...
  94. type HTTPStatusHandler struct {
  95. Debug bool // TODO implement
  96. }
  97. // NewHTTPStatusHandler ...
  98. func NewHTTPStatusHandler() *HTTPStatusHandler {
  99. return &HTTPStatusHandler{
  100. Debug: false,
  101. }
  102. }
  103. // HTTPStatusRecovery catches errors and serves http statuses
  104. func (rec *HTTPStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  105. defer func() {
  106. if err := recover(); err != nil {
  107. // get the stack traces and print it out
  108. // stack := stack(0)
  109. // log.Printf("PANIC: %s\n%s", err, stack)
  110. // convert err to a string
  111. var errMsg string
  112. if e, ok := err.(error); ok {
  113. errMsg = e.Error()
  114. } else {
  115. errMsg = fmt.Sprint(err)
  116. }
  117. // Logging
  118. _, file, line, _ := runtime.Caller(4)
  119. log.Printf("%s:%d: %s", file, line, errMsg)
  120. errText, jErr := json.Marshal(map[string]string{"status": errMsg})
  121. if jErr != nil {
  122. log.Println(jErr)
  123. // TODO handle error
  124. }
  125. if _, ok := err.(MetaError); ok {
  126. http.Error(w, string(errText), err.(MetaError).httpStatus)
  127. } else if _, ok = err.(StatusError); ok {
  128. http.Error(w, string(errText), err.(StatusError).httpStatus)
  129. } else {
  130. http.Error(w, string(errText), http.StatusInternalServerError)
  131. }
  132. }
  133. }()
  134. next(w, r)
  135. }
  136. // Log logs
  137. func Log(text string) {
  138. log.Println(text)
  139. }