smpp-otp/pkg/lib/utils/utils.go

36 lines
702 B
Go
Raw Normal View History

2024-03-27 18:44:50 +00:00
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)
}