forked from mtusi2026/mtusi2026lab4
42 lines
739 B
Go
42 lines
739 B
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mtusi2026lab2/internal/middleware"
|
|
)
|
|
|
|
type errorBody struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
RequestID string `json:"request_id"`
|
|
}
|
|
|
|
type errorResponse struct {
|
|
Error errorBody `json:"error"`
|
|
}
|
|
|
|
func writeJSON(w http.ResponseWriter, status int, data any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
|
|
_ = json.NewEncoder(w).Encode(data)
|
|
}
|
|
|
|
func writeError(
|
|
w http.ResponseWriter,
|
|
r *http.Request,
|
|
status int,
|
|
code string,
|
|
message string,
|
|
) {
|
|
writeJSON(w, status, errorResponse{
|
|
Error: errorBody{
|
|
Code: code,
|
|
Message: message,
|
|
RequestID: middleware.GetRequestID(r.Context()),
|
|
},
|
|
})
|
|
}
|