4
0

errors.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package admin
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/api/render"
  8. )
  9. // ProblemType is the type of the Admin problem.
  10. type ProblemType int
  11. const (
  12. // ErrorNotFoundType resource not found.
  13. ErrorNotFoundType ProblemType = iota
  14. // ErrorAuthorityMismatchType resource Authority ID does not match the
  15. // context Authority ID.
  16. ErrorAuthorityMismatchType
  17. // ErrorDeletedType resource has been deleted.
  18. ErrorDeletedType
  19. // ErrorBadRequestType bad request.
  20. ErrorBadRequestType
  21. // ErrorNotImplementedType not implemented.
  22. ErrorNotImplementedType
  23. // ErrorUnauthorizedType internal server error.
  24. ErrorUnauthorizedType
  25. // ErrorServerInternalType internal server error.
  26. ErrorServerInternalType
  27. )
  28. // String returns the string representation of the admin problem type,
  29. // fulfilling the Stringer interface.
  30. func (ap ProblemType) String() string {
  31. switch ap {
  32. case ErrorNotFoundType:
  33. return "notFound"
  34. case ErrorAuthorityMismatchType:
  35. return "authorityMismatch"
  36. case ErrorDeletedType:
  37. return "deleted"
  38. case ErrorBadRequestType:
  39. return "badRequest"
  40. case ErrorNotImplementedType:
  41. return "notImplemented"
  42. case ErrorUnauthorizedType:
  43. return "unauthorized"
  44. case ErrorServerInternalType:
  45. return "internalServerError"
  46. default:
  47. return fmt.Sprintf("unsupported error type '%d'", int(ap))
  48. }
  49. }
  50. type errorMetadata struct {
  51. details string
  52. status int
  53. typ string
  54. String string
  55. }
  56. var (
  57. errorServerInternalMetadata = errorMetadata{
  58. typ: ErrorServerInternalType.String(),
  59. details: "the server experienced an internal error",
  60. status: 500,
  61. }
  62. errorMap = map[ProblemType]errorMetadata{
  63. ErrorNotFoundType: {
  64. typ: ErrorNotFoundType.String(),
  65. details: "resource not found",
  66. status: http.StatusNotFound,
  67. },
  68. ErrorAuthorityMismatchType: {
  69. typ: ErrorAuthorityMismatchType.String(),
  70. details: "resource not owned by authority",
  71. status: http.StatusUnauthorized,
  72. },
  73. ErrorDeletedType: {
  74. typ: ErrorDeletedType.String(),
  75. details: "resource is deleted",
  76. status: http.StatusNotFound,
  77. },
  78. ErrorNotImplementedType: {
  79. typ: ErrorNotImplementedType.String(),
  80. details: "not implemented",
  81. status: http.StatusNotImplemented,
  82. },
  83. ErrorBadRequestType: {
  84. typ: ErrorBadRequestType.String(),
  85. details: "bad request",
  86. status: http.StatusBadRequest,
  87. },
  88. ErrorUnauthorizedType: {
  89. typ: ErrorUnauthorizedType.String(),
  90. details: "unauthorized",
  91. status: http.StatusUnauthorized,
  92. },
  93. ErrorServerInternalType: errorServerInternalMetadata,
  94. }
  95. )
  96. // Error represents an Admin error
  97. type Error struct {
  98. Type string `json:"type"`
  99. Detail string `json:"detail"`
  100. Message string `json:"message"`
  101. Err error `json:"-"`
  102. Status int `json:"-"`
  103. }
  104. // IsType returns true if the error type matches the input type.
  105. func (e *Error) IsType(pt ProblemType) bool {
  106. return pt.String() == e.Type
  107. }
  108. // NewError creates a new Error type.
  109. func NewError(pt ProblemType, msg string, args ...interface{}) *Error {
  110. return newError(pt, errors.Errorf(msg, args...))
  111. }
  112. func newError(pt ProblemType, err error) *Error {
  113. meta, ok := errorMap[pt]
  114. if !ok {
  115. meta = errorServerInternalMetadata
  116. return &Error{
  117. Type: meta.typ,
  118. Detail: meta.details,
  119. Status: meta.status,
  120. Err: err,
  121. }
  122. }
  123. return &Error{
  124. Type: meta.typ,
  125. Detail: meta.details,
  126. Status: meta.status,
  127. Err: err,
  128. }
  129. }
  130. // NewErrorISE creates a new ErrorServerInternalType Error.
  131. func NewErrorISE(msg string, args ...interface{}) *Error {
  132. return NewError(ErrorServerInternalType, msg, args...)
  133. }
  134. // WrapError attempts to wrap the internal error.
  135. func WrapError(typ ProblemType, err error, msg string, args ...interface{}) *Error {
  136. switch e := err.(type) {
  137. case nil:
  138. return nil
  139. case *Error:
  140. if e.Err == nil {
  141. e.Err = errors.Errorf(msg+"; "+e.Detail, args...)
  142. } else {
  143. e.Err = errors.Wrapf(e.Err, msg, args...)
  144. }
  145. return e
  146. default:
  147. return newError(typ, errors.Wrapf(err, msg, args...))
  148. }
  149. }
  150. // WrapErrorISE shortcut to wrap an internal server error type.
  151. func WrapErrorISE(err error, msg string, args ...interface{}) *Error {
  152. return WrapError(ErrorServerInternalType, err, msg, args...)
  153. }
  154. // StatusCode returns the status code and implements the StatusCoder interface.
  155. func (e *Error) StatusCode() int {
  156. return e.Status
  157. }
  158. // Error allows AError to implement the error interface.
  159. func (e *Error) Error() string {
  160. return e.Err.Error()
  161. }
  162. // Cause returns the internal error and implements the Causer interface.
  163. func (e *Error) Cause() error {
  164. if e.Err == nil {
  165. return errors.New(e.Detail)
  166. }
  167. return e.Err
  168. }
  169. // ToLog implements the EnableLogger interface.
  170. func (e *Error) ToLog() (interface{}, error) {
  171. b, err := json.Marshal(e)
  172. if err != nil {
  173. return nil, WrapErrorISE(err, "error marshaling authority.Error for logging")
  174. }
  175. return string(b), nil
  176. }
  177. // Render implements render.RenderableError for Error.
  178. func (e *Error) Render(w http.ResponseWriter) {
  179. e.Message = e.Err.Error()
  180. render.JSONStatus(w, e, e.StatusCode())
  181. }