MCPcopy
hub / github.com/devtron-labs/devtron / convertToIntWithContext

Function convertToIntWithContext

api/restHandler/common/ParamParserUtils.go:73–94  ·  view source on GitHub ↗

convertToIntWithContext provides better error messages for parameter conversion

(w http.ResponseWriter, paramValue, paramName string)

Source from the content-addressed store, hash-verified

71
72// convertToIntWithContext provides better error messages for parameter conversion
73func convertToIntWithContext(w http.ResponseWriter, paramValue, paramName string) (int, error) {
74 if paramValue == "" {
75 apiErr := util.NewMissingRequiredFieldError(paramName)
76 WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
77 return 0, apiErr
78 }
79
80 paramIntValue, err := strconv.Atoi(paramValue)
81 if err != nil {
82 apiErr := util.NewInvalidPathParameterError(paramName, paramValue)
83 WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
84 return 0, apiErr
85 }
86
87 if paramIntValue < 0 {
88 apiErr := util.NewValidationErrorForField(paramName, "must be a positive integer")
89 WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
90 return 0, apiErr
91 }
92
93 return paramIntValue, nil
94}
95
96func convertToIntArray(paramValue string) ([]int, error) {
97 var paramValues []int

Callers 1

Calls 4

WriteJsonRespFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…