Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #108 from UlfBj/master
Browse files Browse the repository at this point in the history
Error code update.
  • Loading branch information
UlfBj authored Feb 12, 2024
2 parents da028c1 + f8c86b4 commit 1ed11e6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
20 changes: 10 additions & 10 deletions server/vissv2server/serviceMgr/serviceMgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,13 +999,13 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
switch requestMap["action"] {
case "set":
if strings.Contains(requestMap["path"].(string), "[") == true {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Forbidden request", "Set request must only address a single end point.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
ts := setVehicleData(requestMap["path"].(string), requestMap["value"].(string))
if len(ts) == 0 {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Internal error", "Underlying system failed to update.")
utils.SetErrorResponse(requestMap, errorResponseMap, 7, "") //service_unavailable
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
Expand All @@ -1015,7 +1015,7 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
pathArray := unpackPaths(requestMap["path"].(string))
if pathArray == nil {
utils.Error.Printf("Unmarshal of path array failed.")
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Internal error.", "Unmarshal failed on array of paths.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
Expand All @@ -1024,7 +1024,7 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
utils.UnpackFilter(requestMap["filter"], &filterList)
if len(filterList) == 0 {
utils.Error.Printf("Request filter malformed.")
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Bad request", "Request filter malformed.")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
Expand All @@ -1037,7 +1037,7 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
dataPack := getDataPack(pathArray, filterList)
if len(dataPack) == 0 {
utils.Info.Printf("No historic data available")
utils.SetErrorResponse(requestMap, errorResponseMap, "404", "Not found", "Historic data not available.")
utils.SetErrorResponse(requestMap, errorResponseMap, 6, "") //unavailable_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
Expand All @@ -1048,13 +1048,13 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
subscriptionState.RouterId = requestMap["RouterId"].(string)
subscriptionState.Path = unpackPaths(requestMap["path"].(string))
if requestMap["filter"] == nil || requestMap["filter"] == "" {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Filter missing.", "")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
dataChan <- utils.FinalizeMessage(errorResponseMap)
break
}
utils.UnpackFilter(requestMap["filter"], &(subscriptionState.FilterList))
if len(subscriptionState.FilterList) == 0 {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Invalid filter.", "See VISSv2 specification.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
}
if requestMap["gatingId"] != nil {
Expand All @@ -1080,7 +1080,7 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
requestMap["subscriptionId"] = subscriptId
}
}
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Unsubscribe failed.", "Incorrect or missing subscription id.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
case "internal-killsubscriptions":
isRemoved := true
Expand All @@ -1094,12 +1094,12 @@ func ServiceMgrInit(mgrId int, serviceMgrChan chan string, stateStorageType stri
requestMap["action"] = "subscription"
requestMap["requestId"] = nil
requestMap["subscriptionId"] = subscriptionId
utils.SetErrorResponse(requestMap, errorResponseMap, "401", "Token expired or consent cancelled.", "")
utils.SetErrorResponse(requestMap, errorResponseMap, 2, "Token expired or consent cancelled.")
dataChan <- utils.FinalizeMessage(errorResponseMap)
_, subscriptionList = scanAndRemoveListItem(subscriptionList, routerId)
}
default:
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Unknown action.", "")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
dataChan <- utils.FinalizeMessage(errorResponseMap)
} // switch
case <-dummyTicker.C:
Expand Down
20 changes: 10 additions & 10 deletions server/vissv2server/vissv2server.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func getTokenErrorMessage(index int) string {
}

func setTokenErrorResponse(reqMap map[string]interface{}, errorCode int) {
utils.SetErrorResponse(reqMap, errorResponseMap, "400", "Bad Request", getTokenErrorMessage(errorCode))
utils.SetErrorResponse(reqMap, errorResponseMap, 3, getTokenErrorMessage(errorCode))
}

// Sends a message to the Access Token Server to validate the Access Token paths and permissions
Expand Down Expand Up @@ -572,19 +572,19 @@ func serveRequest(request string, tDChanIndex int, sDChanIndex int) {
var requestMap = make(map[string]interface{})
if utils.MapRequest(request, &requestMap) != 0 {
utils.Error.Printf("serveRequest():invalid JSON format=%s", request)
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "invalid request syntax", "See VISSv2 spec and JSON RFC for valid request syntax.")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
if requestMap["action"] == nil || validRequest(request, requestMap["action"].(string)) == false {
utils.Error.Printf("serveRequest():invalid action params=%s", requestMap["action"])
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "invalid request syntax", "Request parameter invalid.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
if requestMap["path"] != nil && strings.Contains(requestMap["path"].(string), "*") == true {
utils.Error.Printf("serveRequest():path contained wildcard=%s", requestMap["path"])
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "invalid request syntax", "Wildcard must be in filter expression.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand All @@ -593,7 +593,7 @@ func serveRequest(request string, tDChanIndex int, sDChanIndex int) {
}
if requestMap["action"] == "set" && requestMap["filter"] != nil {
utils.Error.Printf("serveRequest():Set request combined with filtering.")
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "invalid request", "Set request must not contain filtering.")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand Down Expand Up @@ -625,7 +625,7 @@ func issueServiceRequest(requestMap map[string]interface{}, tDChanIndex int, sDC
err := json.Unmarshal([]byte(filterList[i].Parameter), &searchPath) // Writes in search path all values in filter
if err != nil {
utils.Error.Printf("Unmarshal filter path array failed.")
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Internal error.", "Unmarshall failed on array of paths.")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand Down Expand Up @@ -655,7 +655,7 @@ func issueServiceRequest(requestMap map[string]interface{}, tDChanIndex int, sDC
return
}
utils.Error.Printf("Metadata not available.")
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Bad request", "Metadata not available.")
utils.SetErrorResponse(requestMap, errorResponseMap, 0, "") //bad_request
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand Down Expand Up @@ -689,12 +689,12 @@ func issueServiceRequest(requestMap map[string]interface{}, tDChanIndex int, sDC
maxValidation = utils.GetMaxValidation(int(validation), maxValidation)
}
if totalMatches == 0 {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "No signals matching path.", "")
utils.SetErrorResponse(requestMap, errorResponseMap, 6, "") //unavailable_data
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
if requestMap["action"] == "set" && golib.VSSgetType(searchData[0].NodeHandle) != gomodel.ACTUATOR {
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Illegal command", "Only the actuator node type can be set.")
utils.SetErrorResponse(requestMap, errorResponseMap, 1, "") //invalid_data
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand Down Expand Up @@ -735,7 +735,7 @@ func issueServiceRequest(requestMap map[string]interface{}, tDChanIndex int, sDC
return
}
default: // should not be possible...
utils.SetErrorResponse(requestMap, errorResponseMap, "400", "Access control tag invalid.", "See VISSv2 spec for access control tagging")
utils.SetErrorResponse(requestMap, errorResponseMap, 7, "") //service_unavailable
backendChan[tDChanIndex] <- utils.FinalizeMessage(errorResponseMap)
return
}
Expand Down
36 changes: 35 additions & 1 deletion utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func ExtractFromToken(token string, claim string) string { // TODO remove white
return ""
}

func SetErrorResponse(reqMap map[string]interface{}, errRespMap map[string]interface{}, number string, reason string, message string) {
/*func SetErrorResponse(reqMap map[string]interface{}, errRespMap map[string]interface{}, number string, reason string, message string) {
if reqMap["RouterId"] != nil {
errRespMap["RouterId"] = reqMap["RouterId"]
}
Expand All @@ -176,6 +176,40 @@ func SetErrorResponse(reqMap map[string]interface{}, errRespMap map[string]inter
}
errRespMap["error"] = errMap
errRespMap["ts"] = GetRfcTime()
}*/

//func SetErrorResponse(reqMap map[string]interface{}, errRespMap map[string]interface{}, number string, reason string, message string) {
func SetErrorResponse(reqMap map[string]interface{}, errRespMap map[string]interface{}, errorListIndex int, altErrorMessage string) {
if reqMap["RouterId"] != nil {
errRespMap["RouterId"] = reqMap["RouterId"]
}
if reqMap["action"] != nil {
errRespMap["action"] = reqMap["action"]
}
if reqMap["requestId"] != nil {
errRespMap["requestId"] = reqMap["requestId"]
} else {
delete(errRespMap, "requestId")
}
if reqMap["subscriptionId"] != nil {
errRespMap["subscriptionId"] = reqMap["subscriptionId"]
}
errorMessage := ErrorInfoList[errorListIndex].Message
if len(altErrorMessage) > 0 {
errorMessage = altErrorMessage
}
errMap := map[string]interface{}{
"number": ErrorInfoList[errorListIndex].Number,
"reason": ErrorInfoList[errorListIndex].Reason,
"message": errorMessage,
}
/* errMap := map[string]interface{}{
"number": number,
"reason": reason,
"message": message,
}*/
errRespMap["error"] = errMap
errRespMap["ts"] = GetRfcTime()
}

func FinalizeMessage(responseMap map[string]interface{}) string {
Expand Down
16 changes: 16 additions & 0 deletions utils/managerdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ const (
PB_LEVEL2 = 3 // path is represented by integer index, retrieved from vsspathlist.json
)

type ErrorInformation struct {
Number string
Reason string
Message string
}

var ErrorInfoList [8]ErrorInformation = [8]ErrorInformation{
{"400","bad_request","The request is malformed."},
{"400","invalid_data","Data present in the request is invalid."},
{"401","expired_token","Access token has expired."},
{"401","invalid_token","Access token is invalid."},
{"401","missing_token","Access token is missing."},
{"403","forbidden_request","The server refuses to carry out the request."},
{"404","unavailable_data","The requested data was not found."},
{"503","service_unavailable","The server is temporarily unable to handle the request."}}

var MuxServer = []*http.ServeMux{
http.NewServeMux(), // for app client HTTP sessions
http.NewServeMux(), // for app client WS sessions
Expand Down

0 comments on commit 1ed11e6

Please sign in to comment.