36 lines
702 B
Go
36 lines
702 B
Go
|
|
package utils
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"log/slog"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
func Err(err error) slog.Attr {
|
||
|
|
return slog.Attr{
|
||
|
|
Key: "error",
|
||
|
|
Value: slog.StringValue(err.Error()),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func RespondWithErrorJSON(w http.ResponseWriter, status int, message string) {
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
w.WriteHeader(status)
|
||
|
|
|
||
|
|
jsonError := struct {
|
||
|
|
Status int `json:"status"`
|
||
|
|
Message string `json:"message"`
|
||
|
|
}{
|
||
|
|
Status: status,
|
||
|
|
Message: message,
|
||
|
|
}
|
||
|
|
|
||
|
|
json.NewEncoder(w).Encode(jsonError)
|
||
|
|
}
|
||
|
|
|
||
|
|
func RespondWithJSON(w http.ResponseWriter, status int, data interface{}) {
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
w.WriteHeader(status)
|
||
|
|
json.NewEncoder(w).Encode(data)
|
||
|
|
}
|