1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-02-21 19:48:53 +02:00

Improve error handling (#1263)

This commit is contained in:
Daniel Mieg 2020-03-17 10:54:48 +01:00 committed by GitHub
parent a49d8947c0
commit 81708648e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,6 +26,7 @@ func abapEnvironmentPullGitRepo(config abapEnvironmentPullGitRepoOptions, teleme
client := piperhttp.Client{}
cookieJar, _ := cookiejar.New(nil)
clientOptions := piperhttp.ClientOptions{
Timeout: 30 * time.Second,
CookieJar: cookieJar,
Username: connectionDetails.User,
Password: connectionDetails.Password,
@ -56,11 +57,11 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
// Loging into the ABAP System - getting the x-csrf-token and cookies
var resp, err = getHTTPResponse("HEAD", pullConnectionDetails, nil, client)
defer resp.Body.Close()
if err != nil {
log.Entry().WithField("StatusCode", resp.Status).WithField("ABAP Endpoint", pullConnectionDetails.URL).Error("Authentication on the ABAP system failed")
handleHTTPError(resp, err, "Authentication on the ABAP system failed", pullConnectionDetails)
return uriConnectionDetails, err
}
defer resp.Body.Close()
log.Entry().WithField("StatusCode", resp.Status).WithField("ABAP Endpoint", pullConnectionDetails.URL).Info("Authentication on the ABAP system successfull")
uriConnectionDetails.XCsrfToken = resp.Header.Get("X-Csrf-Token")
pullConnectionDetails.XCsrfToken = uriConnectionDetails.XCsrfToken
@ -68,11 +69,11 @@ func triggerPull(config abapEnvironmentPullGitRepoOptions, pullConnectionDetails
// Trigger the Pull of a Repository
var jsonBody = []byte(`{"sc_name":"` + config.RepositoryName + `"}`)
resp, err = getHTTPResponse("POST", pullConnectionDetails, jsonBody, client)
defer resp.Body.Close()
if err != nil {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, uriConnectionDetails)
return uriConnectionDetails, err
}
defer resp.Body.Close()
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Info("Triggered Pull of Repository / Software Component")
// Parse Response
@ -97,11 +98,11 @@ func pollEntity(config abapEnvironmentPullGitRepoOptions, connectionDetails conn
for {
var resp, err = getHTTPResponse("GET", connectionDetails, nil, client)
defer resp.Body.Close()
if err != nil {
log.Entry().WithField("StatusCode", resp.Status).WithField("repositoryName", config.RepositoryName).Error("Could not pull the Repository / Software Component")
handleHTTPError(resp, err, "Could not pull the Repository / Software Component "+config.RepositoryName, connectionDetails)
return "", err
}
defer resp.Body.Close()
// Parse response
var body abapEntity
@ -199,6 +200,15 @@ func getHTTPResponse(requestType string, connectionDetails connectionDetailsHTTP
return req, err
}
func handleHTTPError(resp *http.Response, err error, message string, connectionDetails connectionDetailsHTTP) {
if resp == nil {
log.Entry().WithError(err).WithField("ABAP Endpoint", connectionDetails.URL).Error("Request failed")
} else {
log.Entry().WithField("StatusCode", resp.Status).Error(message)
resp.Body.Close()
}
}
type abapEntity struct {
Metadata abapMetadata `json:"__metadata"`
UUID string `json:"uuid"`