Commit 2fad9c7a authored by hammad zafar's avatar hammad zafar
Browse files

add error handling capability through ProblemDetails structure

parent b6fba09c
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -30,3 +30,12 @@ func convertPredictedQostoJson(predictedQos *PredictedQos) string {
	}
	return string(jsonInfo)
}

func convertProblemDetailstoJson(probdetails *ProblemDetails) string {
	jsonInfo, err := json.Marshal(*probdetails)
	if err != nil {
		log.Println(err.Error())
		return ""
	}
	return string(jsonInfo)
}
+21 −10
Original line number Diff line number Diff line
@@ -557,20 +557,20 @@ func predictedQosPost(w http.ResponseWriter, r *http.Request) {
	err := json.Unmarshal(bodyBytes, &requestData)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, "Request body is empty.", http.StatusBadRequest)
		return
	}

	// Validating mandatory parameters in request
	if requestData.LocationGranularity == "" {
		log.Error("Mandatory locationGranularity parameter not present")
		http.Error(w, "Mandatory locationGranularity parameter not present", http.StatusBadRequest)
		errHandlerProblemDetails(w, "Mandatory attribute locationGranularity is missing in the request body.", http.StatusBadRequest)
		return
	}

	if requestData.Routes == nil {
		log.Error("Mandatory routes parameter not present")
		http.Error(w, "Mandatory routes parameter not present", http.StatusBadRequest)
	if requestData.Routes == nil || len(requestData.Routes) == 0 {
		log.Error("Mandatory routes parameter is either empty or not present")
		errHandlerProblemDetails(w, "Mandatory attribute routes is either empty or not present in the request.", http.StatusBadRequest)
		return
	}

@@ -579,13 +579,13 @@ func predictedQosPost(w http.ResponseWriter, r *http.Request) {
	for i, route := range requestData.Routes {
		if route.RouteInfo == nil {
			log.Error("Mandatory routeInfo parameter not present in routes")
			http.Error(w, "Mandatory routeInfo parameter not present in routes", http.StatusBadRequest)
			errHandlerProblemDetails(w, "Mandatory attribute routes.routeInfo not present in the request.", http.StatusBadRequest)
			return
		}

		if len(route.RouteInfo) < 2 {
			log.Error("At least two location points required in routeInfo")
			http.Error(w, "At least two location points required in routeInfo", http.StatusBadRequest)
			errHandlerProblemDetails(w, "At least two location points required in routeInfo structure.", http.StatusBadRequest)
			return
		}

@@ -594,20 +594,20 @@ func predictedQosPost(w http.ResponseWriter, r *http.Request) {
			// empty location attribute will cause a runtime error: invalid memory address or nil pointer dereference
			if routeInfo.Location == nil || routeInfo.Location.GeoArea == nil {
				log.Error("Mandatory attribute location is either empty or not present in routeInfo")
				http.Error(w, "Mandatory attribute routes.routeInfo.location is either empty or not present in the request in at least one of the routeInfo structures.", http.StatusBadRequest)
				errHandlerProblemDetails(w, "Mandatory attribute routes.routeInfo.location is either empty or not present in the request in at least one of the routeInfo structures.", http.StatusBadRequest)
				return
			}

			if routeInfo.Location.Ecgi != nil {
				log.Error("Ecgi is not supported in location for MEC Sandbox")
				http.Error(w, "Ecgi is not supported inside location attribute, only geoArea is supported.", http.StatusBadRequest)
				errHandlerProblemDetails(w, "Ecgi is not supported inside routes.routeInfo.location attribute, only geoArea is supported.", http.StatusBadRequest)
				return
			}

			isValidGeoArea := routeInfo.Location.GeoArea != nil && (routeInfo.Location.GeoArea.Latitude == 0 || routeInfo.Location.GeoArea.Longitude == 0)
			if isValidGeoArea {
				log.Error("Mandatory latitude/longitude parameter(s) either not present in geoArea or have a zero value")
				http.Error(w, "At least one of the routeInfo structures either does not contain mandatory latitude/longitude parameter(s) in geoArea or have zero value(s).", http.StatusBadRequest)
				errHandlerProblemDetails(w, "At least one of the routes.routeInfo structures either does not contain mandatory latitude / longitude parameter(s) in geoArea or have zero value(s).", http.StatusBadRequest)
				return
			}

@@ -641,3 +641,14 @@ func predictedQosPost(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, jsonResponse)
}

func errHandlerProblemDetails(w http.ResponseWriter, error string, code int) {
	var pd ProblemDetails
	pd.Detail = error
	pd.Status = int32(code)

	jsonResponse := convertProblemDetailstoJson(&pd)

	w.WriteHeader(code)
	fmt.Fprint(w, jsonResponse)
}