| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 | package logicimport (	"encoding/json"	"net/http"	"github.com/gravitl/netmaker/models"	"golang.org/x/exp/slog")// FormatError - takes ErrorResponse and uses correct codefunc FormatError(err error, errType string) models.ErrorResponse {	var status = http.StatusInternalServerError	switch errType {	case "internal":		status = http.StatusInternalServerError	case "badrequest":		status = http.StatusBadRequest	case "notfound":		status = http.StatusNotFound	case "unauthorized":		status = http.StatusUnauthorized	case "forbidden":		status = http.StatusForbidden	default:		status = http.StatusInternalServerError	}	var response = models.ErrorResponse{		Message: err.Error(),		Code:    status,	}	return response}// ReturnSuccessResponse - processes message and adds headerfunc ReturnSuccessResponse(response http.ResponseWriter, request *http.Request, message string) {	var httpResponse models.SuccessResponse	httpResponse.Code = http.StatusOK	httpResponse.Message = message	response.Header().Set("Content-Type", "application/json")	response.WriteHeader(http.StatusOK)	json.NewEncoder(response).Encode(httpResponse)}// ReturnSuccessResponseWithJson - processes message and adds headerfunc ReturnSuccessResponseWithJson(response http.ResponseWriter, request *http.Request, res interface{}, message string) {	var httpResponse models.SuccessResponse	httpResponse.Code = http.StatusOK	httpResponse.Response = res	httpResponse.Message = message	response.Header().Set("Content-Type", "application/json")	response.WriteHeader(http.StatusOK)	json.NewEncoder(response).Encode(httpResponse)}// ReturnErrorResponse - processes error and adds headerfunc ReturnErrorResponse(response http.ResponseWriter, request *http.Request, errorMessage models.ErrorResponse) {	httpResponse := &models.ErrorResponse{Code: errorMessage.Code, Message: errorMessage.Message}	jsonResponse, err := json.Marshal(httpResponse)	if err != nil {		panic(err)	}	slog.Debug("processed request error", "err", errorMessage.Message)	response.Header().Set("Content-Type", "application/json")	response.WriteHeader(errorMessage.Code)	response.Write(jsonResponse)}
 |