Skip to content

Commit 3be4ac2

Browse files
committed
common function to build error message using cli output and err
1 parent 342426f commit 3be4ac2

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

pkg/RepoManages.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/devtron-labs/git-sensor/internals/sql"
2626
"github.com/devtron-labs/git-sensor/internals/util"
2727
"github.com/devtron-labs/git-sensor/pkg/git"
28+
util2 "github.com/devtron-labs/git-sensor/util"
2829
_ "github.com/robfig/cron/v3"
2930
"go.uber.org/zap"
3031
"strings"
@@ -209,11 +210,7 @@ func (impl RepoManagerImpl) updatePipelineMaterialCommit(gitCtx git.GitContext,
209210
return gitCtx.Err()
210211
} else if err != nil {
211212
pipelineMaterial.Errored = true
212-
if errMsg != "" {
213-
pipelineMaterial.ErrorMsg = errMsg
214-
} else {
215-
pipelineMaterial.ErrorMsg = err.Error()
216-
}
213+
pipelineMaterial.ErrorMsg = util2.BuildDisplayErrorMessage(errMsg, err)
217214
pipelineMaterial.LastSeenHash = ""
218215
} else {
219216
impl.logger.Infow("commits found", "commit", commits)
@@ -377,11 +374,7 @@ func (impl RepoManagerImpl) checkoutMaterial(gitCtx git.GitContext, material *sq
377374
} else if err != nil {
378375
material.CheckoutStatus = false
379376
material.CheckoutMsgAny = err.Error()
380-
if errMsg != "" {
381-
material.FetchErrorMessage = errMsg
382-
} else {
383-
material.FetchErrorMessage = err.Error()
384-
}
377+
material.FetchErrorMessage = util2.BuildDisplayErrorMessage(errMsg, err)
385378
} else {
386379
material.CheckoutLocation = checkoutLocationForFetching
387380
material.CheckoutStatus = true
@@ -683,11 +676,7 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(gitCtx git.GitContext, pipe
683676
if err != nil {
684677
gitMaterial.CheckoutStatus = false
685678
gitMaterial.CheckoutMsgAny = err.Error()
686-
if errMsg != "" {
687-
gitMaterial.FetchErrorMessage = errMsg
688-
} else {
689-
gitMaterial.FetchErrorMessage = err.Error()
690-
}
679+
gitMaterial.FetchErrorMessage = util2.BuildDisplayErrorMessage(errMsg, err)
691680

692681
impl.logger.Errorw("error in fetching the repository ", "gitMaterial", gitMaterial, "err", err)
693682
return nil, err

pkg/git/Watcher.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/devtron-labs/git-sensor/internals"
2929
"github.com/devtron-labs/git-sensor/internals/middleware"
3030
"github.com/devtron-labs/git-sensor/internals/sql"
31+
util2 "github.com/devtron-labs/git-sensor/util"
3132
"github.com/gammazero/workerpool"
3233
"github.com/robfig/cron/v3"
3334
"go.uber.org/zap"
@@ -172,11 +173,7 @@ func (impl GitWatcherImpl) pollAndUpdateGitMaterial(materialReq *sql.GitMaterial
172173
material.FetchStatus = err == nil
173174
if err != nil {
174175
material.LastFetchErrorCount = material.LastFetchErrorCount + 1
175-
if errMsg != "" {
176-
material.FetchErrorMessage = errMsg
177-
} else {
178-
material.FetchErrorMessage = err.Error()
179-
}
176+
material.FetchErrorMessage = util2.BuildDisplayErrorMessage(errMsg, err)
180177
} else {
181178
material.LastFetchErrorCount = 0
182179
material.FetchErrorMessage = ""
@@ -256,11 +253,7 @@ func (impl GitWatcherImpl) pollGitMaterialAndNotify(material *sql.GitMaterial) (
256253
commits, errMsg, err := impl.repositoryManager.ChangesSinceByRepository(gitCtx, repo, material.Value, lastSeenHash, "", fetchCount, checkoutLocation, false)
257254
if err != nil {
258255
material.Errored = true
259-
if errMsg != "" {
260-
material.ErrorMsg = errMsg
261-
} else {
262-
material.ErrorMsg = err.Error()
263-
}
256+
material.ErrorMsg = util2.BuildDisplayErrorMessage(errMsg, err)
264257

265258
erroredMaterialsModels = append(erroredMaterialsModels, material)
266259
} else if len(commits) > 0 {

util/ErrorUtil.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ func IsErrNoRows(err error) bool {
2525
return pg.ErrNoRows == err
2626
}
2727

28+
func BuildDisplayErrorMessage(cliMessage string, err error) string {
29+
customErrorMessage := GetErrMsgFromCliMessage(cliMessage, err)
30+
if customErrorMessage != "" {
31+
return customErrorMessage
32+
} else {
33+
if cliMessage != "" {
34+
return cliMessage
35+
} else {
36+
return err.Error()
37+
}
38+
}
39+
}
40+
2841
// This function returns custom error message. If cliMessage is empty then it checks same handling in err.Error()
2942
func GetErrMsgFromCliMessage(cliMessage string, err error) string {
3043
errMsg := strings.TrimSpace(cliMessage)

0 commit comments

Comments
 (0)