netmaker/logic/errors.go

70 lines
2.1 KiB
Go
Raw Normal View History

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"
NET-1082: Scale Testing Fixes (#2894) * add additional mutex lock on node acls func * increase verbosity * disable acls on cloud emqx * add emqx creds creation to go routine * add debug log of mq client id * comment port check * uncomment port check * check for connection mq connection open * use username for client id * add write mutex on acl is allowed * add mq connection lost handler on server * spin off zombie init as go routine * get whole api path from config * Revert "get whole api path from config" This reverts commit 392f5f4c5f00530788f09d26a655dcbe03a06ccb. * update extclient acls async * add additional mutex lock on node acls func (cherry picked from commit 5325f0e7d7ff9411f497fdc38c980ac0c3a6847d) * increase verbosity (cherry picked from commit 705b3cf0bfbca4d7f5dccdd579875ebb00f85511) * add emqx creds creation to go routine (cherry picked from commit c8e65f4820771eb0c7c7d62b77334211c6b82adb) * add debug log of mq client id (cherry picked from commit 29c5d6cecad6fcaeb4a57bac85895d6516294e28) * comment port check (cherry picked from commit db8d6d95ead39e9f436ad4dbc4176f5ff9312863) * check for connection mq connection open (cherry picked from commit 13b11033b0795693a0a1a0a225db50b8a5c001ae) * use username for client id (cherry picked from commit e90c7386dea48b560c9060289f1db06f2a8d77c1) * add write mutex on acl is allowed (cherry picked from commit 4cae1b0bb4b4b608fdb76b3ef49c7fd7390c9ccb) * add mq connection lost handler on server (cherry picked from commit c82918ad3564098487d5ac4a223ee5d95e76ac3e) * spin off zombie init as go routine (cherry picked from commit 6d65c44c4375ff7a292c05d2becf6507e7310837) * update extclient acls async (cherry picked from commit 6557ef1ebe87ec7c74e9038a99185616a2b87e89) * additionl logs for oauth user flow (cherry picked from commit 61703038ae3227de6f706e1d1d9a9362e4c258d5) * add more debug logs (cherry picked from commit 5980beacd10e7efa1cfbc12e6c048452d9ceca82) * add more debug logs (cherry picked from commit 4d001f0d2709fcc55ae66e2591a0bd079207a61d) * add set auth secret (cherry picked from commit f41cef5da51a884e68d12ac90ae627428b02b112) * fix fetch pass (cherry picked from commit 825caf4b600133f8365f5ab71e0ecd69cc8a996b) * make sure auth secret is set only once (cherry picked from commit ba33ed02aa52237126904f4bbe17b53dd595d7cf) * make sure auth secret is set only once (cherry picked from commit 920ac4c5073fb2c8805520d37abddffb062896ae) * comment usage of emqx acls * replace read lock with write lock on acls * replace read lock with write lock on acls (cherry picked from commit 808d2135c80461edfa96b61070e09ad136fa4644) * use deadlock pkg for visibility * add additional mutex locks * remove race flag * on mq re-connecting donot exit if failed * on mq re-connecting donot exit if failed * revert mutex package change * set mq clean session * remove debug log * go mod tidy * revert on prem emqx acls del
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
}
// 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)
}
NET-1082: Scale Testing Fixes (#2894) * add additional mutex lock on node acls func * increase verbosity * disable acls on cloud emqx * add emqx creds creation to go routine * add debug log of mq client id * comment port check * uncomment port check * check for connection mq connection open * use username for client id * add write mutex on acl is allowed * add mq connection lost handler on server * spin off zombie init as go routine * get whole api path from config * Revert "get whole api path from config" This reverts commit 392f5f4c5f00530788f09d26a655dcbe03a06ccb. * update extclient acls async * add additional mutex lock on node acls func (cherry picked from commit 5325f0e7d7ff9411f497fdc38c980ac0c3a6847d) * increase verbosity (cherry picked from commit 705b3cf0bfbca4d7f5dccdd579875ebb00f85511) * add emqx creds creation to go routine (cherry picked from commit c8e65f4820771eb0c7c7d62b77334211c6b82adb) * add debug log of mq client id (cherry picked from commit 29c5d6cecad6fcaeb4a57bac85895d6516294e28) * comment port check (cherry picked from commit db8d6d95ead39e9f436ad4dbc4176f5ff9312863) * check for connection mq connection open (cherry picked from commit 13b11033b0795693a0a1a0a225db50b8a5c001ae) * use username for client id (cherry picked from commit e90c7386dea48b560c9060289f1db06f2a8d77c1) * add write mutex on acl is allowed (cherry picked from commit 4cae1b0bb4b4b608fdb76b3ef49c7fd7390c9ccb) * add mq connection lost handler on server (cherry picked from commit c82918ad3564098487d5ac4a223ee5d95e76ac3e) * spin off zombie init as go routine (cherry picked from commit 6d65c44c4375ff7a292c05d2becf6507e7310837) * update extclient acls async (cherry picked from commit 6557ef1ebe87ec7c74e9038a99185616a2b87e89) * additionl logs for oauth user flow (cherry picked from commit 61703038ae3227de6f706e1d1d9a9362e4c258d5) * add more debug logs (cherry picked from commit 5980beacd10e7efa1cfbc12e6c048452d9ceca82) * add more debug logs (cherry picked from commit 4d001f0d2709fcc55ae66e2591a0bd079207a61d) * add set auth secret (cherry picked from commit f41cef5da51a884e68d12ac90ae627428b02b112) * fix fetch pass (cherry picked from commit 825caf4b600133f8365f5ab71e0ecd69cc8a996b) * make sure auth secret is set only once (cherry picked from commit ba33ed02aa52237126904f4bbe17b53dd595d7cf) * make sure auth secret is set only once (cherry picked from commit 920ac4c5073fb2c8805520d37abddffb062896ae) * comment usage of emqx acls * replace read lock with write lock on acls * replace read lock with write lock on acls (cherry picked from commit 808d2135c80461edfa96b61070e09ad136fa4644) * use deadlock pkg for visibility * add additional mutex locks * remove race flag * on mq re-connecting donot exit if failed * on mq re-connecting donot exit if failed * revert mutex package change * set mq clean session * remove debug log * go mod tidy * revert on prem emqx acls del
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
}