CHANGE: Change Error To ProblemDetails
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 1m52s
Build, Push and Deploy CMS / build-push-deploy (push) Successful in 1m52s
This commit is contained in:
@@ -10,14 +10,35 @@ func Health(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
}
|
||||
|
||||
// problemDetails is an error body in the RFC 9457 "Problem Details for HTTP
|
||||
// APIs" format. Type stays "about:blank" — the value RFC 9457 defines for
|
||||
// problems with no dedicated documentation URI — which is why Title carries
|
||||
// the plain HTTP status phrase and Detail carries the occurrence-specific
|
||||
// explanation.
|
||||
type problemDetails struct {
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Status int `json:"status"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, body any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
writeJSONContent(w, "application/json", status, body)
|
||||
}
|
||||
|
||||
func writeProblem(w http.ResponseWriter, status int, detail string) {
|
||||
writeJSONContent(w, "application/problem+json", status, problemDetails{
|
||||
Type: "about:blank",
|
||||
Title: http.StatusText(status),
|
||||
Status: status,
|
||||
Detail: detail,
|
||||
})
|
||||
}
|
||||
|
||||
func writeJSONContent(w http.ResponseWriter, contentType string, status int, body any) {
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(body); err != nil {
|
||||
log.Printf("Something Went Wrong Writing The Response: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSONError(w http.ResponseWriter, status int, message string) {
|
||||
writeJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user