Skip to content

Commit

Permalink
ABAP aakaas and build steps: Parse odata error json (#4946)
Browse files Browse the repository at this point in the history
* parseErrorBody Implemented
  • Loading branch information
tiloKo authored Jun 5, 2024
1 parent 0ca2f72 commit df1db9e
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/abap/build/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (conn *Connector) GetToken(appendum string) error {
}
defer response.Body.Close()
errorbody, _ := io.ReadAll(response.Body)
return errors.Wrapf(err, "Fetching X-CSRF-Token failed: %v", string(errorbody))
return errors.Wrapf(err, "Fetching X-CSRF-Token failed: %v", extractErrorStackFromJsonData(errorbody))

}
defer response.Body.Close()
Expand All @@ -85,7 +85,7 @@ func (conn Connector) Get(appendum string) ([]byte, error) {
}
defer response.Body.Close()
errorbody, _ := io.ReadAll(response.Body)
return errorbody, errors.Wrapf(err, "Get failed: %v", string(errorbody))
return errorbody, errors.Wrapf(err, "Get failed: %v", extractErrorStackFromJsonData(errorbody))

}
defer response.Body.Close()
Expand All @@ -109,7 +109,7 @@ func (conn Connector) Post(appendum string, importBody string) ([]byte, error) {
}
defer response.Body.Close()
errorbody, _ := io.ReadAll(response.Body)
return errorbody, errors.Wrapf(err, "Post failed: %v", string(errorbody))
return errorbody, errors.Wrapf(err, "Post failed: %v", extractErrorStackFromJsonData(errorbody))

}
defer response.Body.Close()
Expand Down Expand Up @@ -268,7 +268,7 @@ func (conn Connector) UploadSarFile(appendum string, sarFile []byte) error {
if err != nil {
defer response.Body.Close()
errorbody, _ := io.ReadAll(response.Body)
return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody))
return errors.Wrapf(err, "Upload of SAR file failed: %v", extractErrorStackFromJsonData(errorbody))
}
defer response.Body.Close()
return nil
Expand Down Expand Up @@ -304,7 +304,7 @@ func (conn Connector) UploadSarFileInChunks(appendum string, fileName string, sa
if response != nil && response.Body != nil {
errorbody, _ := io.ReadAll(response.Body)
response.Body.Close()
return errors.Wrapf(err, "Upload of SAR file failed: %v", string(errorbody))
return errors.Wrapf(err, "Upload of SAR file failed: %v", extractErrorStackFromJsonData(errorbody))
} else {
return err
}
Expand Down
68 changes: 68 additions & 0 deletions pkg/abap/build/gwError.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package build

import (
"encoding/json"
"fmt"
"strings"
)

type GW_error struct {
Error struct {
Code string
Message struct {
Lang string
Value string
}
Innererror struct {
Application struct {
Component_id string
Service_namespace string
Service_id string
Service_version string
}
Transactionid string
Timestamp string
Error_Resolution struct {
SAP_Transaction string
SAP_Note string
}
Errordetails []struct {
ContentID string
Code string
Message string
Propertyref string
Severity string
Transition bool
Target string
}
}
}
}

func extractErrorStackFromJsonData(jsonData []byte) string {
my_error := new(GW_error)
if err := my_error.FromJson(jsonData); err != nil {
return string(jsonData)
}
return my_error.ExtractStack()
}

func (my_error *GW_error) FromJson(inputJson []byte) error {
if err := json.Unmarshal(inputJson, my_error); err != nil {
return err
}
return nil
}

func (my_error *GW_error) ExtractStack() string {
var stack strings.Builder
var previousMessage string
for index, detail := range my_error.Error.Innererror.Errordetails {
if previousMessage == detail.Message {
continue
}
previousMessage = detail.Message
stack.WriteString(fmt.Sprintf("[%v] %s\n", index, detail.Message))
}
return stack.String()
}
117 changes: 117 additions & 0 deletions pkg/abap/build/gwError_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package build

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestExtractErrorStack(t *testing.T) {
t.Run("Valid Json String", func(t *testing.T) {
//act
errorString := extractErrorStackFromJsonData([]byte(validResponse))
//assert
assert.Equal(t, `[0] Data Provider: SOME_PACKAGE and 0 other packages do not exist in system XXX
[1] Reading SCs of Packages failed
`, errorString)
})

t.Run("No Json String", func(t *testing.T) {
//arrange
var noJson = "ERROR 404: Unauthorized"
//act
errorString := extractErrorStackFromJsonData([]byte(noJson))
//assert
assert.Equal(t, noJson, errorString)
})

t.Run("step by step", func(t *testing.T) {
my_error := new(GW_error)
err := my_error.FromJson([]byte(validResponse))
assert.NoError(t, err)

assert.Equal(t, "/IWBEP/CM_MGW_RT/022", my_error.Error.Code)
assert.Equal(t, "en", my_error.Error.Message.Lang)
assert.Equal(t, "Data Provider: SOME_PACKAGE and 0 other packages do not exist in system XXX", my_error.Error.Message.Value)
assert.Equal(t, "BC-UPG-ADDON", my_error.Error.Innererror.Application.Component_id)
assert.Equal(t, "/BUILD/", my_error.Error.Innererror.Application.Service_namespace)
assert.Equal(t, "CORE_SRV", my_error.Error.Innererror.Application.Service_id)
assert.Equal(t, "0001", my_error.Error.Innererror.Application.Service_version)
assert.Equal(t, "1801B32D512B00C0E0066215B8D723B5", my_error.Error.Innererror.Transactionid)
assert.Equal(t, "", my_error.Error.Innererror.Timestamp)
assert.Equal(t, "", my_error.Error.Innererror.Error_Resolution.SAP_Transaction)
assert.Equal(t, "See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)", my_error.Error.Innererror.Error_Resolution.SAP_Note)
assert.Equal(t, 3, len(my_error.Error.Innererror.Errordetails))
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[0].ContentID)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[0].Propertyref)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[0].Target)
assert.Equal(t, "error", my_error.Error.Innererror.Errordetails[0].Severity)
assert.Equal(t, false, my_error.Error.Innererror.Errordetails[0].Transition)
assert.Equal(t, "/BUILD/CX_EXTERNAL", my_error.Error.Innererror.Errordetails[0].Code)
assert.Equal(t, "Data Provider: SOME_PACKAGE and 0 other packages do not exist in system XXX", my_error.Error.Innererror.Errordetails[0].Message)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[1].ContentID)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[1].Propertyref)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[1].Target)
assert.Equal(t, "error", my_error.Error.Innererror.Errordetails[1].Severity)
assert.Equal(t, false, my_error.Error.Innererror.Errordetails[1].Transition)
assert.Equal(t, "/BUILD/CX_BUILD", my_error.Error.Innererror.Errordetails[1].Code)
assert.Equal(t, "Reading SCs of Packages failed", my_error.Error.Innererror.Errordetails[1].Message)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[2].ContentID)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[2].Propertyref)
assert.Equal(t, "", my_error.Error.Innererror.Errordetails[2].Target)
assert.Equal(t, "error", my_error.Error.Innererror.Errordetails[2].Severity)
assert.Equal(t, false, my_error.Error.Innererror.Errordetails[2].Transition)
assert.Equal(t, "/IWBEP/CX_MGW_BUSI_EXCEPTION", my_error.Error.Innererror.Errordetails[2].Code)
assert.Equal(t, "Reading SCs of Packages failed", my_error.Error.Innererror.Errordetails[2].Message)
})
}

var validResponse = `{
"error": {
"code": "/IWBEP/CM_MGW_RT/022",
"message": {
"lang": "en",
"value": "Data Provider: SOME_PACKAGE and 0 other packages do not exist in system XXX"
},
"innererror": {
"application": {
"component_id": "BC-UPG-ADDON",
"service_namespace": "/BUILD/",
"service_id": "CORE_SRV",
"service_version": "0001"
},
"transactionid": "1801B32D512B00C0E0066215B8D723B5",
"timestamp": "",
"Error_Resolution": {
"SAP_Transaction": "",
"SAP_Note": "See SAP Note 1797736 for error analysis (https://service.sap.com/sap/support/notes/1797736)"
},
"errordetails": [{
"ContentID": "",
"code": "/BUILD/CX_EXTERNAL",
"message": "Data Provider: SOME_PACKAGE and 0 other packages do not exist in system XXX",
"propertyref": "",
"severity": "error",
"transition": false,
"target": ""
}, {
"ContentID": "",
"code": "/BUILD/CX_BUILD",
"message": "Reading SCs of Packages failed",
"propertyref": "",
"severity": "error",
"transition": false,
"target": ""
}, {
"ContentID": "",
"code": "/IWBEP/CX_MGW_BUSI_EXCEPTION",
"message": "Reading SCs of Packages failed",
"propertyref": "",
"severity": "error",
"transition": false,
"target": ""
}
]
}
}
}`

0 comments on commit df1db9e

Please sign in to comment.