Skip to content

Commit 2ad59e1

Browse files
errors Improve the error that is returned when a response cannot be unmarshalled as JSON (#104)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Proprietary --> ### Description <!-- Please add any detail or context that would be useful to a reviewer. --> Improve the error that is returned when a response cannot be unmarshalled as JSON ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent 152e5fd commit 2ad59e1

File tree

5 files changed

+21
-1
lines changed

5 files changed

+21
-1
lines changed

changes/20250407100502.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: `errors` Improve the error that is returned when a response cannot be unmarshalled as JSON

utils/api/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"strings"
1414

1515
"github.com/ARM-software/embedded-development-services-client-utils/utils/errors"
16+
"github.com/ARM-software/golang-utils/utils/commonerrors"
1617
"github.com/ARM-software/golang-utils/utils/parallelisation"
1718
"github.com/ARM-software/golang-utils/utils/reflection"
1819
)
@@ -40,7 +41,7 @@ func CheckAPICallSuccess(ctx context.Context, errorContext string, resp *_http.R
4041
if resp != nil {
4142
statusCode = resp.StatusCode
4243
errorDetails, subErr := errors.FetchAPIErrorDescriptionWithContext(ctx, resp)
43-
if subErr != nil {
44+
if commonerrors.Ignore(subErr, commonerrors.ErrMarshalling) != nil {
4445
err = subErr
4546
return
4647
}

utils/api/api_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func TestCheckAPICallSuccess(t *testing.T) {
5858
assert.Equal(t, actualErr.Error(), expectedErr)
5959
})
6060

61+
t.Run("api call not successful (no JSON response)", func(t *testing.T) {
62+
errMessage := "response error"
63+
parentCtx := context.Background()
64+
resp := _http.Response{StatusCode: 403, Body: io.NopCloser(bytes.NewReader([]byte("<html><head><title>403 Forbidden</title></head></html>")))}
65+
actualErr := CheckAPICallSuccess(parentCtx, errMessage, &resp, errors.New("403 Forbidden"))
66+
expectedErr := "response error (403): <html><head><title>403 Forbidden</title></head></html>; 403 Forbidden"
67+
assert.Equal(t, actualErr.Error(), expectedErr)
68+
})
69+
6170
t.Run("no context error, api call successful", func(t *testing.T) {
6271
errMessage := "no error"
6372
parentCtx := context.Background()

utils/errors/errors.go

Lines changed: 2 additions & 0 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/commonerrors"
1718
"github.com/ARM-software/golang-utils/utils/parallelisation"
1819
"github.com/ARM-software/golang-utils/utils/reflection"
1920
"github.com/ARM-software/golang-utils/utils/safeio"
@@ -51,6 +52,7 @@ func FetchAPIErrorDescriptionWithContext(ctx context.Context, resp *_http.Respon
5152
}
5253
if err != nil {
5354
message = string(bytes)
55+
err = commonerrors.WrapError(commonerrors.ErrMarshalling, err, "error occurred when unmarshalling response")
5456
return
5557
}
5658
apiErrorMessage := strings.Builder{}

utils/errors/errors_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ func TestFetchAPIErrorDescription(t *testing.T) {
4545
expectedMessage := ""
4646
assert.Equal(t, expectedMessage, actualMessage)
4747
})
48+
49+
t.Run("error response is not JSON", func(t *testing.T) {
50+
resp := _http.Response{Body: io.NopCloser(bytes.NewReader([]byte("<html><head><title>403 Forbidden</title></head></html>")))}
51+
actualMessage := FetchAPIErrorDescription(&resp)
52+
expectedMessage := "<html><head><title>403 Forbidden</title></head></html>"
53+
assert.Equal(t, expectedMessage, actualMessage)
54+
})
4855
}
4956

5057
func TestFetchAPIErrorDescriptionInterrupt(t *testing.T) {

0 commit comments

Comments
 (0)