30 lines
587 B
Go
30 lines
587 B
Go
package response
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
type ErrorBody struct {
|
|
Error ErrorDetail `json:"error"`
|
|
}
|
|
|
|
type ErrorDetail struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
func JSON(w http.ResponseWriter, status int, value any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(value)
|
|
}
|
|
|
|
func Error(w http.ResponseWriter, status int, code string, err error) {
|
|
msg := code
|
|
if err != nil {
|
|
msg = err.Error()
|
|
}
|
|
JSON(w, status, ErrorBody{Error: ErrorDetail{Code: code, Message: msg}})
|
|
}
|