This repository has been archived on 2026-07-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
codespace/pkg/response/response.go
T
2026-07-02 15:07:54 +08:00

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}})
}