4
0

errors.go 5.2 KB

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