Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add details for EOF errors #4442

Merged
merged 12 commits into from
Jul 18, 2023
19 changes: 9 additions & 10 deletions cmd/abapEnvironmentCloneGitRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func abapEnvironmentCloneGitRepo(config abapEnvironmentCloneGitRepoOptions, _ *t
}

client := piperhttp.Client{}

// error situations should stop execution through log.Entry().Fatal() call which leads to an os.Exit(1) in the end
err := runAbapEnvironmentCloneGitRepo(&config, &autils, &client)
if err != nil {
Expand Down Expand Up @@ -76,9 +75,9 @@ func runAbapEnvironmentCloneGitRepo(config *abapEnvironmentCloneGitRepoOptions,
logString := repo.GetCloneLogString()
errorString := "Clone of " + logString + " failed on the ABAP system"

log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()
log.Entry().Info("Start cloning " + logString)
log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()

// Triggering the Clone of the repository into the ABAP Environment system
uriConnectionDetails, errorTriggerClone, didCheckoutPullInstead := triggerClone(repo, connectionDetails, client)
Expand All @@ -99,7 +98,7 @@ func runAbapEnvironmentCloneGitRepo(config *abapEnvironmentCloneGitRepoOptions,
log.Entry().Info("The " + logString + " was cloned successfully")
}
}
log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()
log.Entry().Info("All repositories were cloned successfully")
return nil
}
Expand Down Expand Up @@ -191,12 +190,12 @@ func handleCloneError(resp *http.Response, err error, cloneConnectionDetails aba
// As an intermediate workaround, we react to the error message A4C_A2G/257 that gets thrown, if the repository had already been cloned
// In this case, a checkout branch and a pull will be performed
alreadyCloned = true
log.Entry().Infof("-------------------------")
log.Entry().Infof("-------------------------")
abaputils.AddDefaultDashedLine()
abaputils.AddDefaultDashedLine()
log.Entry().Infof("%s", "The repository / software component has already been cloned on the ABAP Environment system ")
log.Entry().Infof("%s", "A `checkout branch` and a `pull` will be performed instead")
log.Entry().Infof("-------------------------")
log.Entry().Infof("-------------------------")
abaputils.AddDefaultDashedLine()
abaputils.AddDefaultDashedLine()
checkoutOptions := abapEnvironmentCheckoutBranchOptions{
Username: cloneConnectionDetails.User,
Password: cloneConnectionDetails.Password,
Expand All @@ -214,8 +213,8 @@ func handleCloneError(resp *http.Response, err error, cloneConnectionDetails aba
if returnedError != nil {
return
}
log.Entry().Infof("-------------------------")
log.Entry().Infof("-------------------------")
abaputils.AddDefaultDashedLine()
abaputils.AddDefaultDashedLine()
pullOptions := abapEnvironmentPullGitRepoOptions{
Username: cloneConnectionDetails.User,
Password: cloneConnectionDetails.Password,
Expand Down
6 changes: 3 additions & 3 deletions cmd/abapEnvironmentPullGitRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func handlePull(repo abaputils.Repository, pullConnectionDetails abaputils.Conne
logString := repo.GetPullLogString()
errorString := "Pull of the " + logString + " failed on the ABAP system"

log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()
log.Entry().Info("Start pulling the " + logString)
log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()

uriConnectionDetails, err := triggerPull(repo, pullConnectionDetails, client)
if err != nil {
Expand Down Expand Up @@ -183,7 +183,7 @@ func checkPullRepositoryConfiguration(options abapEnvironmentPullGitRepoOptions)
}

func finishPullLogs() {
log.Entry().Info("-------------------------")
abaputils.AddDefaultDashedLine()
log.Entry().Info("All repositories were pulled successfully")
}

Expand Down
21 changes: 20 additions & 1 deletion pkg/abaputils/abaputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,20 @@ func HandleHTTPError(resp *http.Response, err error, message string, connectionD
if resp == nil {
// Response is nil in case of a timeout
log.Entry().WithError(err).WithField("ABAP Endpoint", connectionDetails.URL).Error("Request failed")

match, _ := regexp.MatchString(".*EOF$", err.Error())
if match {
AddDefaultDashedLine()
log.Entry().Infof("%s", "A connection could not be established to the ABAP system. The typical root cause is the network configuration (firewall, IP allowlist, etc.)")
AddDefaultDashedLine()
}

log.Entry().Infof("Error message: %s,", err.Error())
} else {

defer resp.Body.Close()

log.Entry().WithField("StatusCode", resp.Status).Error(message)
log.Entry().WithField("StatusCode", resp.Status).WithField("User", connectionDetails.User).WithField("URL", connectionDetails.URL).Error(message)

errorText, errorCode, parsingError := GetErrorDetailsFromResponse(resp)
if parsingError != nil {
Expand Down Expand Up @@ -239,6 +248,16 @@ func ConvertTime(logTimeStamp string) time.Time {
return t
}

// AddDefaultDashedLine adds 25 dashes
func AddDefaultDashedLine() {
log.Entry().Infof(strings.Repeat("-", 25))
}

// AddDefaultDebugLine adds 25 dashes in debug
func AddDebugDashedLine() {
log.Entry().Debugf(strings.Repeat("-", 25))
}

/*******************************
* Structs for specific steps *
*******************************/
Expand Down
18 changes: 18 additions & 0 deletions pkg/abaputils/abaputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/pkg/errors"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -350,4 +351,21 @@ func TestHandleHTTPError(t *testing.T) {
assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
log.Entry().Info(err.Error())
})

t.Run("EOF Error", func(t *testing.T) {

message := "Custom Error Message"
errorValue := "Received Error EOF"
receivedErr := errors.New(errorValue)

_, hook := test.NewNullLogger()
log.RegisterHook(hook)

err := HandleHTTPError(nil, receivedErr, message, ConnectionDetailsHTTP{})

assert.EqualError(t, err, fmt.Sprintf("%s", receivedErr.Error()))
assert.Equal(t, 5, len(hook.Entries), "Expected a different number of entries")
assert.Equal(t, `A connection could not be established to the ABAP system. The typical root cause is the network configuration (firewall, IP allowlist, etc.)`, hook.AllEntries()[2].Message, "Expected a different message")
hook.Reset()
})
}
10 changes: 5 additions & 5 deletions pkg/abaputils/manageGitRepositoryUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func PrintLogs(repositoryName string, connectionDetails ConnectionDetailsHTTP, c
for _, logEntryForDetails := range entity.ToLogOverview.Results {
printLog(logEntryForDetails, connectionDetails, client)
}
log.Entry().Infof("-------------------------")
AddDefaultDashedLine()

return
}
Expand Down Expand Up @@ -151,14 +151,14 @@ func allLogsHaveBeenPrinted(entity LogProtocolResults, page int, err error) bool
func printHeader(logEntry LogResultsV2) {
if logEntry.Status != `Success` {
log.Entry().Infof("\n")
log.Entry().Infof("-------------------------")
AddDefaultDashedLine()
log.Entry().Infof("%s (%v)", logEntry.Name, ConvertTime(logEntry.Timestamp))
log.Entry().Infof("-------------------------")
AddDefaultDashedLine()
} else {
log.Entry().Debugf("\n")
log.Entry().Debugf("-------------------------")
AddDebugDashedLine()
log.Entry().Debugf("%s (%v)", logEntry.Name, ConvertTime(logEntry.Timestamp))
log.Entry().Debugf("-------------------------")
AddDebugDashedLine()
}
}

Expand Down
Loading