json.go 3.5 KB

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