@@ -20,11 +20,14 @@ import (
2020 "bytes"
2121 "context"
2222 "encoding/json"
23+ "errors"
2324 "fmt"
2425 "github.com/devtron-labs/common-lib/utils/k8s"
26+ "github.com/devtron-labs/devtron/internal/util"
2527 "github.com/devtron-labs/devtron/pkg/cluster/repository"
2628 "go.uber.org/zap"
2729 "io/ioutil"
30+ k8sError "k8s.io/apimachinery/pkg/api/errors"
2831 "k8s.io/apimachinery/pkg/runtime"
2932 "k8s.io/apimachinery/pkg/runtime/schema"
3033 "k8s.io/apimachinery/pkg/runtime/serializer"
@@ -117,7 +120,7 @@ func (impl ArgoK8sClientImpl) CreateArgoApplication(ctx context.Context, namespa
117120 if err != nil {
118121 return fmt .Errorf ("error creating argo cd app" )
119122 }
120- impl .logger .Infow ("creating application" , "req " , application )
123+ impl .logger .Debugw ("creating argo application resource " , "application " , application )
121124 res , err := client .
122125 Post ().
123126 Resource ("applications" ).
@@ -126,23 +129,48 @@ func (impl ArgoK8sClientImpl) CreateArgoApplication(ctx context.Context, namespa
126129 Do (ctx ).Raw ()
127130
128131 if err != nil {
129- response := make (map [string ]interface {})
130- err := json .Unmarshal (res , & response )
131- if err != nil {
132- impl .logger .Errorw ("unmarshal error on app update status" , "err" , err )
133- return fmt .Errorf ("error creating argo cd app" )
134- }
135- message := "error creating argo cd app"
136- if response != nil && response ["message" ] != nil {
137- message = response ["message" ].(string )
138- }
139- return fmt .Errorf (message )
132+ impl .logger .Errorw ("error in argo application resource creation" , "namespace" , namespace , "res" , res , "err" , err )
133+ return impl .handleArgoAppCreationError (res , err )
140134 }
141135
142- impl .logger .Infow ("argo app create res " , "res " , string ( res ) , "err " , err )
136+ impl .logger .Infow ("argo app create successfully " , "namespace " , namespace , "res " , string ( res ) )
143137 return err
144138}
145139
140+ func (impl ArgoK8sClientImpl ) handleArgoAppCreationError (res []byte , err error ) error {
141+ // default error set
142+ apiError := & util.ApiError {
143+ InternalMessage : "error creating argo cd app" ,
144+ UserMessage : "error creating argo cd app" ,
145+ }
146+ // error override for errors.StatusError
147+ if statusError := (& k8sError.StatusError {}); errors .As (err , & statusError ) {
148+ apiError .HttpStatusCode = int (statusError .Status ().Code )
149+ apiError .InternalMessage = statusError .Error ()
150+ apiError .UserMessage = statusError .Error ()
151+ }
152+ response := make (map [string ]interface {})
153+ jsonErr := json .Unmarshal (res , & response )
154+ if jsonErr != nil {
155+ impl .logger .Errorw ("unmarshal error on app update status" , "err" , jsonErr )
156+ return apiError
157+ }
158+ // error override if API response exists, as response errors are more readable
159+ if response != nil {
160+ if statusCode , ok := response ["code" ]; apiError .HttpStatusCode == 0 && ok {
161+ if statusCodeFloat , ok := statusCode .(float64 ); ok {
162+ apiError .HttpStatusCode = int (statusCodeFloat )
163+ }
164+ }
165+ if response ["message" ] != nil {
166+ errMsg := response ["message" ].(string )
167+ apiError .InternalMessage = errMsg
168+ apiError .UserMessage = errMsg
169+ }
170+ }
171+ return apiError
172+ }
173+
146174func (impl ArgoK8sClientImpl ) GetArgoApplication (namespace string , appName string , cluster * repository.Cluster ) (map [string ]interface {}, error ) {
147175
148176 config , err := rest .InClusterConfig ()
0 commit comments