json.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package json // import "miniflux.app/http/response/json"
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "net/http"
  9. "miniflux.app/http/response"
  10. "miniflux.app/logger"
  11. )
  12. const contentTypeHeader = `application/json`
  13. // OK creates a new JSON response with a 200 status code.
  14. func OK(w http.ResponseWriter, r *http.Request, body interface{}) {
  15. builder := response.New(w, r)
  16. builder.WithHeader("Content-Type", contentTypeHeader)
  17. builder.WithBody(toJSON(body))
  18. builder.Write()
  19. }
  20. // Created sends a created response to the client.
  21. func Created(w http.ResponseWriter, r *http.Request, body interface{}) {
  22. builder := response.New(w, r)
  23. builder.WithStatus(http.StatusCreated)
  24. builder.WithHeader("Content-Type", contentTypeHeader)
  25. builder.WithBody(toJSON(body))
  26. builder.Write()
  27. }
  28. // NoContent sends a no content response to the client.
  29. func NoContent(w http.ResponseWriter, r *http.Request) {
  30. builder := response.New(w, r)
  31. builder.WithStatus(http.StatusNoContent)
  32. builder.WithHeader("Content-Type", contentTypeHeader)
  33. builder.Write()
  34. }
  35. // ServerError sends an internal error to the client.
  36. func ServerError(w http.ResponseWriter, r *http.Request, err error) {
  37. logger.Error("[HTTP:Internal Server Error] %s => %v", r.URL, err)
  38. builder := response.New(w, r)
  39. builder.WithStatus(http.StatusInternalServerError)
  40. builder.WithHeader("Content-Type", contentTypeHeader)
  41. builder.WithBody(toJSONError(err))
  42. builder.Write()
  43. }
  44. // BadRequest sends a bad request error to the client.
  45. func BadRequest(w http.ResponseWriter, r *http.Request, err error) {
  46. logger.Error("[HTTP:Bad Request] %s => %v", r.URL, err)
  47. builder := response.New(w, r)
  48. builder.WithStatus(http.StatusBadRequest)
  49. builder.WithHeader("Content-Type", contentTypeHeader)
  50. builder.WithBody(toJSONError(err))
  51. builder.Write()
  52. }
  53. // Unauthorized sends a not authorized error to the client.
  54. func Unauthorized(w http.ResponseWriter, r *http.Request) {
  55. logger.Error("[HTTP:Unauthorized] %s", r.URL)
  56. builder := response.New(w, r)
  57. builder.WithStatus(http.StatusUnauthorized)
  58. builder.WithHeader("Content-Type", contentTypeHeader)
  59. builder.WithBody(toJSONError(errors.New("Access Unauthorized")))
  60. builder.Write()
  61. }
  62. // Forbidden sends a forbidden error to the client.
  63. func Forbidden(w http.ResponseWriter, r *http.Request) {
  64. logger.Error("[HTTP:Forbidden] %s", r.URL)
  65. builder := response.New(w, r)
  66. builder.WithStatus(http.StatusForbidden)
  67. builder.WithHeader("Content-Type", contentTypeHeader)
  68. builder.WithBody(toJSONError(errors.New("Access Forbidden")))
  69. builder.Write()
  70. }
  71. // NotFound sends a page not found error to the client.
  72. func NotFound(w http.ResponseWriter, r *http.Request) {
  73. logger.Error("[HTTP:Not Found] %s", r.URL)
  74. builder := response.New(w, r)
  75. builder.WithStatus(http.StatusNotFound)
  76. builder.WithHeader("Content-Type", contentTypeHeader)
  77. builder.WithBody(toJSONError(errors.New("Resource Not Found")))
  78. builder.Write()
  79. }
  80. func toJSONError(err error) []byte {
  81. type errorMsg struct {
  82. ErrorMessage string `json:"error_message"`
  83. }
  84. return toJSON(errorMsg{ErrorMessage: err.Error()})
  85. }
  86. func toJSON(v interface{}) []byte {
  87. b, err := json.Marshal(v)
  88. if err != nil {
  89. logger.Error("[HTTP:JSON] %v", err)
  90. return []byte("")
  91. }
  92. return b
  93. }