Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/appStore/chartGroup/ChartGroupRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (impl *ChartGroupRestHandlerImpl) GetChartGroupWithChartMetaData(w http.Res
}

// Use enhanced parameter parsing with context
chartGroupId, err := common.ExtractIntPathParamWithContext(w, r, "chartGroupId", "chart group")
chartGroupId, err := common.ExtractIntPathParamWithContext(w, r, "chartGroupId")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -223,7 +223,7 @@ func (impl *ChartGroupRestHandlerImpl) GetChartGroupInstallationDetail(w http.Re
}

// Use enhanced parameter parsing with context
chartGroupId, err := common.ExtractIntPathParamWithContext(w, r, "chartGroupId", "chart group")
chartGroupId, err := common.ExtractIntPathParamWithContext(w, r, "chartGroupId")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
6 changes: 3 additions & 3 deletions api/auth/user/UserRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Reques
}

// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "user")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -334,7 +334,7 @@ func (handler UserRestHandlerImpl) DeleteUser(w http.ResponseWriter, r *http.Req
}

// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "user")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -427,7 +427,7 @@ func (handler UserRestHandlerImpl) BulkDeleteUsers(w http.ResponseWriter, r *htt

func (handler UserRestHandlerImpl) FetchRoleGroupById(w http.ResponseWriter, r *http.Request) {
// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "role group")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
2 changes: 1 addition & 1 deletion api/chartRepo/ChartRepositoryRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (handler *ChartRepositoryRestHandlerImpl) GetChartRepoById(w http.ResponseW
}

// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "chart repository")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
4 changes: 2 additions & 2 deletions api/cluster/ClusterRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ func (impl ClusterRestHandlerImpl) FindByIds(w http.ResponseWriter, r *http.Requ

func (impl ClusterRestHandlerImpl) FindById(w http.ResponseWriter, r *http.Request) {
// Use enhanced parameter parsing with context
clusterId, err := common.ExtractIntPathParamWithContext(w, r, "id", "cluster")
clusterId, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -734,7 +734,7 @@ func (impl ClusterRestHandlerImpl) GetClusterNamespaces(w http.ResponseWriter, r
isActionUserSuperAdmin = true
}
// extract cluster and handle response on error
clusterId, err := common.ExtractIntPathParamWithContext(w, r, "clusterId", "cluster")
clusterId, err := common.ExtractIntPathParamWithContext(w, r, "clusterId")
if err != nil {
impl.logger.Error("error in parsing clusterId", "clusterId", clusterId, "err", err)
return
Expand Down
2 changes: 1 addition & 1 deletion api/cluster/EnvironmentRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (impl EnvironmentRestHandlerImpl) Update(w http.ResponseWriter, r *http.Req

func (impl EnvironmentRestHandlerImpl) FindById(w http.ResponseWriter, r *http.Request) {
// Use enhanced parameter parsing with context
envId, err := common.ExtractIntPathParamWithContext(w, r, "id", "environment")
envId, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
51 changes: 3 additions & 48 deletions api/middleware/ErrorHandlingMiddleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ package middleware
import (
"context"
"fmt"
"github.com/devtron-labs/devtron/internal/util"
"github.com/gorilla/mux"
"go.uber.org/zap"
"net/http"
"strconv"
"time"

"github.com/gorilla/mux"
"go.uber.org/zap"
)

// ErrorHandlingMiddleware provides enhanced error handling and logging for REST handlers
Expand Down Expand Up @@ -156,47 +155,3 @@ func GetRequestContext(r *http.Request) *RequestContext {
}
return nil
}

// LogError logs an error with request context for better debugging
func (m *ErrorHandlingMiddleware) LogError(r *http.Request, err error, operation string) {
reqCtx := GetRequestContext(r)
if reqCtx != nil {
m.logger.Errorw("Request error",
"requestId", reqCtx.RequestID,
"operation", operation,
"error", err,
"method", reqCtx.Method,
"path", reqCtx.Path,
"resourceType", reqCtx.ResourceType,
"resourceId", reqCtx.ResourceID,
)
} else {
m.logger.Errorw("Request error",
"operation", operation,
"error", err,
"method", r.Method,
"path", r.URL.Path,
)
}
}

// ValidateIntPathParam validates and extracts an integer path parameter with enhanced error handling
func ValidateIntPathParam(r *http.Request, paramName string) (int, *util.ApiError) {
vars := mux.Vars(r)
paramValue := vars[paramName]

if paramValue == "" {
return 0, util.NewMissingRequiredFieldError(paramName)
}

id, err := strconv.Atoi(paramValue)
if err != nil {
return 0, util.NewInvalidPathParameterError(paramName, paramValue)
}

if id <= 0 {
return 0, util.NewValidationErrorForField(paramName, "must be a positive integer")
}

return id, nil
}
6 changes: 2 additions & 4 deletions api/restHandler/CoreAppRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,9 @@ func (handler CoreAppRestHandlerImpl) GetAppAllDetail(w http.ResponseWriter, r *
return
}

vars := mux.Vars(r)
appId, err := strconv.Atoi(vars["appId"])
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId")
if err != nil {
handler.logger.Errorw("request err, GetAppAllDetail", "err", err, "appId", vars["appId"])
common.WriteInvalidAppIdError(w, vars["appId"])
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
return
}

Expand Down
8 changes: 4 additions & 4 deletions api/restHandler/NotificationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ func (impl NotificationRestHandlerImpl) DeleteNotificationSettings(w http.Respon

func (impl NotificationRestHandlerImpl) GetAllNotificationSettings(w http.ResponseWriter, r *http.Request) {
// Use enhanced parameter parsing with context
size, err := common.ExtractIntPathParamWithContext(w, r, "size", "pagination")
size, err := common.ExtractIntPathParamWithContext(w, r, "size")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
}

offset, err := common.ExtractIntPathParamWithContext(w, r, "offset", "pagination")
offset, err := common.ExtractIntPathParamWithContext(w, r, "offset")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -522,7 +522,7 @@ func (impl NotificationRestHandlerImpl) FindSESConfig(w http.ResponseWriter, r *
}

// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "SES config")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -551,7 +551,7 @@ func (impl NotificationRestHandlerImpl) FindSlackConfig(w http.ResponseWriter, r
}

// Use enhanced parameter parsing with context
id, err := common.ExtractIntPathParamWithContext(w, r, "id", "Slack config")
id, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
2 changes: 1 addition & 1 deletion api/restHandler/app/appInfo/AppInfoRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (handler AppInfoRestHandlerImpl) GetAppMetaInfo(w http.ResponseWriter, r *h
return
}
// Use enhanced parameter parsing with context
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId", "application")
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
4 changes: 2 additions & 2 deletions api/restHandler/app/pipeline/AutoCompleteRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (handler DevtronAppAutoCompleteRestHandlerImpl) GetAppListForAutocomplete(w
return
}
} else {
teamIdInt, err = common.ExtractIntPathParamWithContext(w, r, "teamId", teamId+" team")
teamIdInt, err = common.ExtractIntPathParamWithContext(w, r, "teamId")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down Expand Up @@ -246,7 +246,7 @@ func (handler DevtronAppAutoCompleteRestHandlerImpl) GitListAutocomplete(w http.
func (handler DevtronAppAutoCompleteRestHandlerImpl) RegistriesListAutocomplete(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("token")
// Use enhanced parameter parsing with context
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId", "application")
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1211,15 +1211,15 @@ func (handler *PipelineConfigRestHandlerImpl) GetCIPipelineById(w http.ResponseW
token := r.Header.Get("token")
// vars := mux.Vars(r)
//appId, err := strconv.Atoi(vars["appId"])
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId", "app")
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId")
if err != nil {
// response already written by ExtractIntPathParamWithContext
//common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Error("request err getting , GetCIPipelineById", "appId", appId, "err", err)
return
}
//pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineId, err := common.ExtractIntPathParamWithContext(w, r, "pipelineId", "pipeline")
pipelineId, err := common.ExtractIntPathParamWithContext(w, r, "pipelineId")
if err != nil {
// response already written by ExtractIntPathParamWithContext
// common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
Expand Down Expand Up @@ -1719,23 +1719,23 @@ func (handler *PipelineConfigRestHandlerImpl) FetchWorkflowDetails(w http.Respon
}
// vars := mux.Vars(r)
// appId, err = strconv.Atoi(vars["appId"])
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId", "app")
appId, err := common.ExtractIntPathParamWithContext(w, r, "appId")
if err != nil {
// response already written by ExtractIntPathParamWithContext
// common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Error("request err getting , FetchWorkflowDetails", "appId", appId, "err", err)
return
}
// pipelineId, err := strconv.Atoi(vars["pipelineId"])
pipelineId, err := common.ExtractIntPathParamWithContext(w, r, "pipelineId", "pipeline")
pipelineId, err := common.ExtractIntPathParamWithContext(w, r, "pipelineId")
if err != nil {
// response already written by ExtractIntPathParamWithContext
// common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
handler.Logger.Error("request err getting , FetchWorkflowDetails", "pipelineId", pipelineId, "err", err)
return
}
// buildId, err := strconv.Atoi(vars["workflowId"])
buildId, err := common.ExtractIntPathParamWithContext(w, r, "workflowId", "workflow")
buildId, err := common.ExtractIntPathParamWithContext(w, r, "workflowId")
if err != nil || buildId == 0 {
// response already written by ExtractIntPathParamWithContext
// common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
Expand Down
51 changes: 6 additions & 45 deletions api/restHandler/common/ParamParserUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,21 @@
package common

import (
"github.com/devtron-labs/devtron/internal/util"
"github.com/gorilla/mux"
"net/http"
"strconv"
"strings"

"github.com/devtron-labs/devtron/internal/util"
"github.com/gorilla/mux"
)

const TokenHeaderKey = "token"

func ExtractIntPathParam(w http.ResponseWriter, r *http.Request, paramName string) (int, error) {
vars := mux.Vars(r)
paramValue := vars[paramName]
paramIntValue, err := convertToInt(w, paramValue)
if err != nil {
return 0, err
}
return paramIntValue, nil
}

// ExtractIntPathParamWithContext provides enhanced error messages with resource context
func ExtractIntPathParamWithContext(w http.ResponseWriter, r *http.Request, paramName string, resourceType string) (int, error) {
func ExtractIntPathParamWithContext(w http.ResponseWriter, r *http.Request, paramName string) (int, error) {
vars := mux.Vars(r)
paramValue := vars[paramName]
paramIntValue, err := convertToIntWithContext(w, paramValue, paramName, resourceType)
paramIntValue, err := convertToIntWithContext(w, paramValue, paramName)
if err != nil {
return 0, err
}
Expand All @@ -57,7 +48,7 @@ func convertToInt(w http.ResponseWriter, paramValue string) (int, error) {
}

// convertToIntWithContext provides better error messages for parameter conversion
func convertToIntWithContext(w http.ResponseWriter, paramValue, paramName, resourceType string) (int, error) {
func convertToIntWithContext(w http.ResponseWriter, paramValue, paramName string) (int, error) {
if paramValue == "" {
apiErr := util.NewMissingRequiredFieldError(paramName)
WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
Expand Down Expand Up @@ -129,33 +120,3 @@ func ExtractBoolQueryParam(r *http.Request, paramName string) (bool, error) {

return boolValue, nil
}

// ExtractIntArrayFromQueryParam returns list of all ids in []int extracted from query param
// use this method over ExtractIntArrayQueryParam if there is list of query params
func ExtractIntArrayFromQueryParam(r *http.Request, paramName string) ([]int, error) {
queryParams := r.URL.Query()
paramValue := queryParams[paramName]
paramIntValues := make([]int, 0)
var err error
if paramValue != nil && len(paramValue) > 0 {
if strings.Contains(paramValue[0], ",") {
paramIntValues, err = convertToIntArray(paramValue[0])
} else {
paramIntValues, err = convertStringArrayToIntArray(paramValue)
}
}

return paramIntValues, err
}

func convertStringArrayToIntArray(strArr []string) ([]int, error) {
var paramValues []int
for _, item := range strArr {
paramIntValue, err := strconv.Atoi(item)
if err != nil {
return paramValues, err
}
paramValues = append(paramValues, paramIntValue)
}
return paramValues, nil
}
8 changes: 2 additions & 6 deletions api/restHandler/common/ResourceContextExtractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package common

import (
"fmt"
"github.com/devtron-labs/devtron/internal/util"
"net/http"
"strconv"
"strings"

"github.com/devtron-labs/devtron/internal/util"
)

// extractResourceContext tries to extract resource type and ID from response body
Expand Down Expand Up @@ -107,8 +108,3 @@ func WriteUnauthorizedError(w http.ResponseWriter) {
WithCode("11010")
WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
}

func WriteInvalidAppIdError(w http.ResponseWriter, appId string) {
apiErr := util.NewInvalidPathParameterError("appId", appId)
WriteJsonResp(w, apiErr, nil, apiErr.HttpStatusCode)
}
2 changes: 1 addition & 1 deletion api/team/TeamRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (impl TeamRestHandlerImpl) FetchAll(w http.ResponseWriter, r *http.Request)

func (impl TeamRestHandlerImpl) FetchOne(w http.ResponseWriter, r *http.Request) {
// Use enhanced parameter parsing with context
teamId, err := common.ExtractIntPathParamWithContext(w, r, "id", "team")
teamId, err := common.ExtractIntPathParamWithContext(w, r, "id")
if err != nil {
// Error already written by ExtractIntPathParamWithContext
return
Expand Down
Loading