Commit 3809bb97 authored by muhammadh's avatar muhammadh
Browse files

add error handling capabilities in problemDetails in service-mgmt

parent 678777e7
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -412,6 +412,9 @@ components:
          type: string
          format: uri
          description: A URI reference that identifies the specific occurrence of the problem
      required:
      - status
      - detail
    GrantType:
      description: OAuth 2.0 grant type
      type: string
+9 −0
Original line number Diff line number Diff line
@@ -86,3 +86,12 @@ func convertSubscriptionLinkListToJson(obj *SubscriptionLinkList) string {
	}
	return string(jsonInfo)
}

func convertProblemDetailstoJson(probdetails *ProblemDetails) string {
	jsonInfo, err := json.Marshal(*probdetails)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}
+2 −2
Original line number Diff line number Diff line
@@ -29,9 +29,9 @@ type ProblemDetails struct {
	// A short, human-readable summary of the problem type
	Title string `json:"title,omitempty"`
	// The HTTP status code for this occurrence of the problem
	Status int32 `json:"status,omitempty"`
	Status int32 `json:"status"`
	// A human-readable explanation specific to this occurrence of the problem
	Detail string `json:"detail,omitempty"`
	Detail string `json:"detail"`
	// A URI reference that identifies the specific occurrence of the problem
	Instance string `json:"instance,omitempty"`
}
+62 −51
Original line number Diff line number Diff line
@@ -186,7 +186,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -198,7 +198,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -215,7 +215,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
	err = decoder.Decode(&sInfoPost)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -223,38 +223,38 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
	if sInfoPost.SerInstanceId != "" {
		errStr := "Service instance ID must not be present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.SerName == "" {
		errStr := "Mandatory Service Name parameter not present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.Version == "" {
		errStr := "Mandatory Service Version parameter not present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.State == nil {
		errStr := "Mandatory Service State parameter not present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.Serializer == nil {
		errStr := "Mandatory Serializer parameter not present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.SerCategory != nil {
		errStr := validateCategoryRef(sInfoPost.SerCategory)
		if errStr != "" {
			log.Error(errStr)
			http.Error(w, errStr, http.StatusBadRequest)
			errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
			return
		}
	}
@@ -262,7 +262,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
		(sInfoPost.TransportId == "" && sInfoPost.TransportInfo == nil) {
		errStr := "Either transportId or transportInfo but not both shall be present"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}
	if sInfoPost.TransportInfo != nil {
@@ -274,7 +274,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
			sInfoPost.TransportInfo.Endpoint == nil {
			errStr := "Id, Name, Type, Protocol, Version, Endpoint are all mandatory parameters of TransportInfo"
			log.Error(errStr)
			http.Error(w, errStr, http.StatusBadRequest)
			errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
			return
		}
	}
@@ -296,7 +296,7 @@ func appServicesPOST(w http.ResponseWriter, r *http.Request) {
	err, retCode := setService(appId, sInfo, ServiceAvailabilityNotificationChangeType_ADDED)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), retCode)
		errHandlerProblemDetails(w, err.Error(), retCode)
		return
	}

@@ -319,7 +319,7 @@ func appServicesByIdPUT(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -331,7 +331,7 @@ func appServicesByIdPUT(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -357,7 +357,7 @@ func appServicesByIdPUT(w http.ResponseWriter, r *http.Request) {
	err = decoder.Decode(&sInfo)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -374,7 +374,7 @@ func appServicesByIdPUT(w http.ResponseWriter, r *http.Request) {
	if sInfoJson != sInfoPrevJson {
		errStr := "Only the ServiceInfo state property may be changed"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusBadRequest)
		errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
		return
	}

@@ -384,7 +384,7 @@ func appServicesByIdPUT(w http.ResponseWriter, r *http.Request) {
		err, retCode := setService(appId, &sInfo, ServiceAvailabilityNotificationChangeType_STATE_CHANGED)
		if err != nil {
			log.Error(err.Error())
			http.Error(w, err.Error(), retCode)
			errHandlerProblemDetails(w, err.Error(), retCode)
			return
		}
	}
@@ -407,7 +407,7 @@ func appServicesByIdDELETE(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -419,7 +419,7 @@ func appServicesByIdDELETE(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -436,7 +436,7 @@ func appServicesByIdDELETE(w http.ResponseWriter, r *http.Request) {
	// Delete service
	err = delServiceById(appId, svcId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -464,7 +464,7 @@ func appServicesGET(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfoAnyMep(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -476,7 +476,7 @@ func appServicesGET(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -497,7 +497,7 @@ func appServicesByIdGET(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfoAnyMep(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -509,7 +509,7 @@ func appServicesByIdGET(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -550,7 +550,7 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
	// Get App instance
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -562,7 +562,7 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -573,19 +573,19 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
	err = decoder.Decode(&serAvailNotifSub)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Validate mandatory properties
	if serAvailNotifSub.CallbackReference == "" {
		log.Error("Mandatory CallbackReference parameter not present")
		http.Error(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
		errHandlerProblemDetails(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
		return
	}
	if serAvailNotifSub.SubscriptionType != SER_AVAILABILITY_NOTIF_SUB_TYPE {
		log.Error("SubscriptionType shall be SerAvailabilityNotificationSubscription")
		http.Error(w, "SubscriptionType shall be SerAvailabilityNotificationSubscription", http.StatusBadRequest)
		errHandlerProblemDetails(w, "SubscriptionType shall be SerAvailabilityNotificationSubscription", http.StatusBadRequest)
		return
	}

@@ -607,7 +607,7 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
				errStr := validateCategoryRef(&categoryRef)
				if errStr != "" {
					log.Error(errStr)
					http.Error(w, errStr, http.StatusBadRequest)
					errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
					return
				}
			}
@@ -619,7 +619,7 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
		if nbMutuallyExclusiveParams > 1 {
			errStr := "FilteringCriteria attributes serInstanceIds, serNames, serCategories are mutually-exclusive"
			log.Error(errStr)
			http.Error(w, errStr, http.StatusBadRequest)
			errHandlerProblemDetails(w, errStr, http.StatusBadRequest)
			return
		}
	}
@@ -640,7 +640,7 @@ func applicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
	_, err = subMgr.CreateSubscription(subCfg, jsonSub)
	if err != nil {
		log.Error("Failed to create subscription")
		http.Error(w, "Failed to create subscription", http.StatusInternalServerError)
		errHandlerProblemDetails(w, "Failed to create subscription", http.StatusInternalServerError)
		return
	}

@@ -662,7 +662,7 @@ func applicationsSubscriptionGET(w http.ResponseWriter, r *http.Request) {
	// Get App instance info
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -674,7 +674,7 @@ func applicationsSubscriptionGET(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -683,7 +683,7 @@ func applicationsSubscriptionGET(w http.ResponseWriter, r *http.Request) {
	sub, err := subMgr.GetSubscription(subId)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -691,7 +691,7 @@ func applicationsSubscriptionGET(w http.ResponseWriter, r *http.Request) {
	if sub.Cfg.AppId != appId || sub.Cfg.Type != SER_AVAILABILITY_NOTIF_SUB_TYPE {
		err = errors.New("Subscription not found")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -712,7 +712,7 @@ func applicationsSubscriptionDELETE(w http.ResponseWriter, r *http.Request) {
	// Get App instance info
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -724,7 +724,7 @@ func applicationsSubscriptionDELETE(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -733,7 +733,7 @@ func applicationsSubscriptionDELETE(w http.ResponseWriter, r *http.Request) {
	sub, err := subMgr.GetSubscription(subId)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -741,7 +741,7 @@ func applicationsSubscriptionDELETE(w http.ResponseWriter, r *http.Request) {
	if sub.Cfg.AppId != appId || sub.Cfg.Type != SER_AVAILABILITY_NOTIF_SUB_TYPE {
		err = errors.New("Subscription not found")
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -749,7 +749,7 @@ func applicationsSubscriptionDELETE(w http.ResponseWriter, r *http.Request) {
	err = subMgr.DeleteSubscription(sub)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -768,7 +768,7 @@ func applicationsSubscriptionsGET(w http.ResponseWriter, r *http.Request) {
	// Get App instance info
	appInfo, err := getAppInfo(appId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusNotFound)
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -780,7 +780,7 @@ func applicationsSubscriptionsGET(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(code)
			fmt.Fprintf(w, problemDetails)
		} else {
			http.Error(w, err.Error(), code)
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}
@@ -839,7 +839,7 @@ func transportsGET(w http.ResponseWriter, r *http.Request) {
	jsonResponse, err := json.Marshal(transportInfoResp)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
@@ -978,7 +978,7 @@ func getServices(w http.ResponseWriter, r *http.Request, appId string) {
	validParams := []string{"ser_instance_id", "ser_name", "ser_category_id", "consumed_local_only", "is_local", "scope_of_locality"}
	err := validateQueryParams(q, validParams)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

@@ -1002,7 +1002,7 @@ func getServices(w http.ResponseWriter, r *http.Request, appId string) {
	// Make sure only 1 or none of the following are present: ser_instance_id, ser_name, ser_category_id
	err = validateServiceQueryParams(serInstanceId, serName, serCategoryId)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

@@ -1031,7 +1031,7 @@ func getServices(w http.ResponseWriter, r *http.Request, appId string) {
	err = rc.ForEachJSONEntry(key, populateServiceInfoList, sInfoList)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -1039,7 +1039,7 @@ func getServices(w http.ResponseWriter, r *http.Request, appId string) {
	jsonResponse, err := json.Marshal(sInfoList.Services)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
@@ -1051,7 +1051,7 @@ func getService(w http.ResponseWriter, r *http.Request, appId string, serviceId
	if serviceId == "" {
		errStr := "Invalid Service ID"
		log.Error(errStr)
		http.Error(w, errStr, http.StatusInternalServerError)
		errHandlerProblemDetails(w, errStr, http.StatusInternalServerError)
		return
	}

@@ -1068,7 +1068,7 @@ func getService(w http.ResponseWriter, r *http.Request, appId string, serviceId
	err := rc.ForEachJSONEntry(key, populateServiceInfoList, &sInfoList)
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -1082,7 +1082,7 @@ func getService(w http.ResponseWriter, r *http.Request, appId string, serviceId
	jsonResponse, err := json.Marshal(sInfoList.Services[0])
	if err != nil {
		log.Error(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteHeader(http.StatusOK)
@@ -1485,3 +1485,14 @@ func newSerAvailabilityNotifSubCfg(sub *SerAvailabilityNotificationSubscription,
	}
	return subCfg
}

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)
}