Skip to content

Commit 273a2ec

Browse files
committed
various improvements
1 parent 666db38 commit 273a2ec

File tree

11 files changed

+31
-22
lines changed

11 files changed

+31
-22
lines changed

changes/20221223152046.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved error formatting

changes/20221223152507.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use new resource definition

utils/api/api.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ func CheckAPICallSuccess(ctx context.Context, errorContext string, resp *_http.R
4040
statusCode = resp.StatusCode
4141
errorDetails := errors.FetchAPIErrorDescription(resp)
4242
if !reflection.IsEmpty(errorDetails) {
43-
errorMessage.WriteString("further details: ")
4443
errorMessage.WriteString(errorDetails)
4544
}
4645
_ = resp.Body.Close()

utils/api/api_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestCheckAPICallSuccess(t *testing.T) {
5454
parentCtx := context.Background()
5555
resp := _http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewReader([]byte("{\"message\": \"client error\",\"requestId\": \"761761721\"}")))}
5656
actualErr := CheckAPICallSuccess(parentCtx, errMessage, &resp, errors.New(errMessage))
57-
expectedErr := "client error (400): further details: client error(request-id: 761761721); client error"
57+
expectedErr := "client error (400): API call error [request-id: 761761721] client error; client error"
5858
assert.Equal(t, actualErr.Error(), expectedErr)
5959
})
6060

utils/errors/errors.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/ARM-software/embedded-development-services-client/client"
17+
"github.com/ARM-software/golang-utils/utils/reflection"
1718
)
1819

1920
// FetchAPIErrorDescription returns the error message from an API response.
@@ -33,13 +34,22 @@ func FetchAPIErrorDescription(resp *_http.Response) (message string) {
3334
return
3435
}
3536
apiErrorMessage := strings.Builder{}
37+
apiErrorMessage.WriteString("API call error")
38+
if code, _ := errorResponse.GetHttpStatusCodeOk(); !reflection.IsEmpty(code) {
39+
apiErrorMessage.WriteString(fmt.Sprintf(" (%v)", *code))
40+
}
41+
apiErrorMessage.WriteString(fmt.Sprintf(" [request-id: %v] ", errorResponse.GetRequestId()))
3642
apiErrorMessage.WriteString(errorResponse.GetMessage())
37-
apiErrorMessage.WriteString(fmt.Sprintf("(request-id: %v)", errorResponse.GetRequestId()))
3843
if fields, has := errorResponse.GetFieldsOk(); has {
3944
apiErrorMessage.WriteString(" [")
45+
start := true
4046
for i := range fields {
47+
if !start {
48+
apiErrorMessage.WriteString(",")
49+
}
4150
field := fields[i]
42-
apiErrorMessage.WriteString(fmt.Sprintf("%v: %v (%v),", field.GetFieldName(), field.GetMessage(), field.GetFieldPath()))
51+
apiErrorMessage.WriteString(fmt.Sprintf("%v: %v (%v)", field.GetFieldName(), field.GetMessage(), field.GetFieldPath()))
52+
start = false
4353
}
4454
apiErrorMessage.WriteString("]")
4555
}

utils/errors/errors_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ func TestFetchAPIErrorDescription(t *testing.T) {
1717
t.Run("error response exists", func(*testing.T) {
1818
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\": \"client error\",\"requestId\": \"761761721\"}")))}
1919
actualMessage := FetchAPIErrorDescription(&resp)
20-
expectedMessage := "client error(request-id: 761761721)"
20+
expectedMessage := "API call error [request-id: 761761721] client error"
2121
assert.Equal(t, actualMessage, expectedMessage)
2222
})
2323

2424
t.Run("error response has fields", func(*testing.T) {
2525
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("{\"message\":\"client error\",\"requestId\":\"761761721\",\"fields\":[{\"fieldName\":\"client request error\",\"fieldPath\":\"https://foo.bar\",\"message\":\"client error\"}]}")))}
2626
actualMessage := FetchAPIErrorDescription(&resp)
27-
expectedMessage := "client error(request-id: 761761721) [client request error: client error (https://foo.bar),]"
27+
expectedMessage := "API call error [request-id: 761761721] client error [client request error: client error (https://foo.bar)]"
2828
assert.Equal(t, actualMessage, expectedMessage)
2929
})
3030

utils/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ARM-software/embedded-development-services-client-utils/utils
33
go 1.19
44

55
require (
6-
github.com/ARM-software/embedded-development-services-client/client v1.15.0
6+
github.com/ARM-software/embedded-development-services-client/client v1.16.0
77
github.com/ARM-software/golang-utils/utils v1.22.0
88
github.com/bxcodec/faker/v3 v3.8.1
99
github.com/go-logr/logr v0.4.0

utils/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
3636
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
3737
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
39-
github.com/ARM-software/embedded-development-services-client/client v1.15.0 h1:2Hs/p42noTEKGoBCLHB7bIwN0eQ7aBFNQWOzGQkPrSc=
40-
github.com/ARM-software/embedded-development-services-client/client v1.15.0/go.mod h1:0QCu7cmtoJ2LXh7Ydu6Uh1Hmk2grKkgmFRNCpG5Sq8M=
39+
github.com/ARM-software/embedded-development-services-client/client v1.16.0 h1:ILY9LviJtr5P53Ar0eMDhRj8hgbBjaFJ51w4MzBylTc=
40+
github.com/ARM-software/embedded-development-services-client/client v1.16.0/go.mod h1:0QCu7cmtoJ2LXh7Ydu6Uh1Hmk2grKkgmFRNCpG5Sq8M=
4141
github.com/ARM-software/golang-utils/utils v1.22.0 h1:TTIxKeHIA/JLbYsUIZsDh0p55hgVLKI/2kbe45qkizs=
4242
github.com/ARM-software/golang-utils/utils v1.22.0/go.mod h1:n4gb8bx3dQEFIb2m0CI2fLuIfQHNGaZ2x9pPLC2cKvo=
4343
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

utils/logging/logger.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,22 @@ func (c *ClientLogger) LogResource(r resource.IResource) {
8989
if r == nil {
9090
c.LogErrorAndMessage(commonerrors.ErrUndefined, "missing resource")
9191
} else {
92-
title, err := r.GetTitle()
92+
title, err := r.FetchTitle()
9393
if err != nil {
9494
c.LogErrorAndMessage(err, "could not retrieve resource's title")
9595
return
9696
}
97-
name, err := r.GetName()
97+
name, err := r.FetchName()
9898
if err != nil {
9999
c.LogErrorAndMessage(err, "could not retrieve resource's name")
100100
return
101101
}
102-
links, err := r.GetLinks()
102+
links, err := r.FetchLinks()
103103
if err != nil {
104104
c.LogErrorAndMessage(err, "could not retrieve resource's links [%v]", title)
105105
return
106106
}
107-
c.LogInfo("Resource (%v): %v [%v] ; affordances': %v", r.GetType(), title, name, serialiseLink(links))
107+
c.LogInfo("Resource (%v): %v [%v] ; affordances': %v", r.FetchType(), title, name, serialiseLink(links))
108108
}
109109
}
110110

utils/resource/interfaces.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
// Package resource provides helpers related to API resources.
77
package resource
88

9+
import "github.com/ARM-software/embedded-development-services-client/client"
10+
911
// IResource describe an API resource.
1012
type IResource interface {
11-
// GetLinks returns the resource links
12-
GetLinks() (any, error)
13-
GetName() (string, error)
14-
GetTitle() (string, error)
15-
GetType() string
13+
client.IModel
1614
}

0 commit comments

Comments
 (0)