2022-09-15 01:26:31 +08:00
|
|
|
package logic
|
2021-04-13 11:19:01 +08:00
|
|
|
|
|
|
|
import (
|
2021-04-23 04:52:44 +08:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/models"
|
2024-04-11 23:48:57 +08:00
|
|
|
"golang.org/x/exp/slog"
|
2021-04-13 11:19:01 +08:00
|
|
|
)
|
|
|
|
|
2022-09-15 01:26:31 +08:00
|
|
|
// FormatError - takes ErrorResponse and uses correct code
|
|
|
|
func FormatError(err error, errType string) models.ErrorResponse {
|
2021-04-13 11:19:01 +08:00
|
|
|
|
|
|
|
var status = http.StatusInternalServerError
|
|
|
|
switch errType {
|
|
|
|
case "internal":
|
|
|
|
status = http.StatusInternalServerError
|
|
|
|
case "badrequest":
|
2021-04-23 04:52:44 +08:00
|
|
|
status = http.StatusBadRequest
|
2021-04-13 11:19:01 +08:00
|
|
|
case "notfound":
|
|
|
|
status = http.StatusNotFound
|
|
|
|
case "unauthorized":
|
|
|
|
status = http.StatusUnauthorized
|
|
|
|
case "forbidden":
|
|
|
|
status = http.StatusForbidden
|
|
|
|
default:
|
|
|
|
status = http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
|
2021-04-23 04:52:44 +08:00
|
|
|
var response = models.ErrorResponse{
|
|
|
|
Message: err.Error(),
|
|
|
|
Code: status,
|
|
|
|
}
|
2021-04-13 11:19:01 +08:00
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:26:31 +08:00
|
|
|
// ReturnSuccessResponse - processes message and adds header
|
|
|
|
func ReturnSuccessResponse(response http.ResponseWriter, request *http.Request, message string) {
|
2021-04-23 04:52:44 +08:00
|
|
|
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)
|
2021-04-13 11:19:01 +08:00
|
|
|
}
|
|
|
|
|
2023-11-30 00:10:07 +08:00
|
|
|
// ReturnSuccessResponseWithJson - processes message and adds header
|
|
|
|
func 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)
|
|
|
|
}
|
|
|
|
|
2022-09-15 01:26:31 +08:00
|
|
|
// ReturnErrorResponse - processes error and adds header
|
|
|
|
func ReturnErrorResponse(response http.ResponseWriter, request *http.Request, errorMessage models.ErrorResponse) {
|
2021-04-23 04:52:44 +08:00
|
|
|
httpResponse := &models.ErrorResponse{Code: errorMessage.Code, Message: errorMessage.Message}
|
|
|
|
jsonResponse, err := json.Marshal(httpResponse)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-04-11 23:48:57 +08:00
|
|
|
slog.Debug("processed request error", "err", errorMessage.Message)
|
2021-04-23 04:52:44 +08:00
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
response.WriteHeader(errorMessage.Code)
|
|
|
|
response.Write(jsonResponse)
|
2021-04-13 11:19:01 +08:00
|
|
|
}
|