4
0

render.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Package render implements functionality related to response rendering.
  2. package render
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "net/http"
  7. "google.golang.org/protobuf/encoding/protojson"
  8. "google.golang.org/protobuf/proto"
  9. "github.com/smallstep/certificates/api/log"
  10. )
  11. // JSON is shorthand for JSONStatus(w, v, http.StatusOK).
  12. func JSON(w http.ResponseWriter, v interface{}) {
  13. JSONStatus(w, v, http.StatusOK)
  14. }
  15. // JSONStatus marshals v into w. It additionally sets the status code of
  16. // w to the given one.
  17. //
  18. // JSONStatus sets the Content-Type of w to application/json unless one is
  19. // specified.
  20. func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
  21. setContentTypeUnlessPresent(w, "application/json")
  22. w.WriteHeader(status)
  23. if err := json.NewEncoder(w).Encode(v); err != nil {
  24. var errUnsupportedType *json.UnsupportedTypeError
  25. if errors.As(err, &errUnsupportedType) {
  26. panic(err)
  27. }
  28. var errUnsupportedValue *json.UnsupportedValueError
  29. if errors.As(err, &errUnsupportedValue) {
  30. panic(err)
  31. }
  32. var errMarshalError *json.MarshalerError
  33. if errors.As(err, &errMarshalError) {
  34. panic(err)
  35. }
  36. }
  37. log.EnabledResponse(w, v)
  38. }
  39. // ProtoJSON is shorthand for ProtoJSONStatus(w, m, http.StatusOK).
  40. func ProtoJSON(w http.ResponseWriter, m proto.Message) {
  41. ProtoJSONStatus(w, m, http.StatusOK)
  42. }
  43. // ProtoJSONStatus writes the given value into the http.ResponseWriter and the
  44. // given status is written as the status code of the response.
  45. func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
  46. b, err := protojson.Marshal(m)
  47. if err != nil {
  48. panic(err)
  49. }
  50. setContentTypeUnlessPresent(w, "application/json")
  51. w.WriteHeader(status)
  52. _, _ = w.Write(b)
  53. }
  54. func setContentTypeUnlessPresent(w http.ResponseWriter, contentType string) {
  55. const header = "Content-Type"
  56. h := w.Header()
  57. if _, ok := h[header]; !ok {
  58. h.Set(header, contentType)
  59. }
  60. }
  61. // RenderableError is the set of errors that implement the basic Render method.
  62. //
  63. // Errors that implement this interface will use their own Render method when
  64. // being rendered into responses.
  65. type RenderableError interface {
  66. error
  67. Render(http.ResponseWriter)
  68. }
  69. // Error marshals the JSON representation of err to w. In case err implements
  70. // RenderableError its own Render method will be called instead.
  71. func Error(w http.ResponseWriter, err error) {
  72. log.Error(w, err)
  73. var r RenderableError
  74. if errors.As(err, &r) {
  75. r.Render(w)
  76. return
  77. }
  78. JSONStatus(w, err, statusCodeFromError(err))
  79. }
  80. // StatusCodedError is the set of errors that implement the basic StatusCode
  81. // function.
  82. //
  83. // Errors that implement this interface will use the code reported by StatusCode
  84. // as the HTTP response code when being rendered by this package.
  85. type StatusCodedError interface {
  86. error
  87. StatusCode() int
  88. }
  89. func statusCodeFromError(err error) (code int) {
  90. code = http.StatusInternalServerError
  91. type causer interface {
  92. Cause() error
  93. }
  94. for err != nil {
  95. var sc StatusCodedError
  96. if errors.As(err, &sc) {
  97. code = sc.StatusCode()
  98. break
  99. }
  100. var c causer
  101. if !errors.As(err, &c) {
  102. break
  103. }
  104. err = c.Cause()
  105. }
  106. return
  107. }