-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
101 lines (94 loc) · 3.2 KB
/
errors.go
File metadata and controls
101 lines (94 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
// Package errors provides common helpers related to errors coming from the web services.
package errors
import (
"context"
"encoding/json"
"fmt"
_http "net/http"
"strings"
"github.com/ARM-software/embedded-development-services-client/client"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/parallelisation"
"github.com/ARM-software/golang-utils/utils/reflection"
"github.com/ARM-software/golang-utils/utils/safeio"
)
// FetchAPIErrorDescription returns the error message from an API response.
// This function does not close the response body.
func FetchAPIErrorDescription(resp *_http.Response) (message string) {
message, _ = FetchAPIErrorDescriptionWithContext(context.Background(), resp)
return
}
// FetchAPIErrorDescriptionWithContext returns the error message from an API response.
// This function does not close the response body.
func FetchAPIErrorDescriptionWithContext(ctx context.Context, resp *_http.Response) (message string, err error) {
if resp == nil {
return
}
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
errorResponse := client.ErrorResponse{}
bytes, err := safeio.ReadAll(ctx, resp.Body)
if err != nil {
return
}
err = json.Unmarshal(bytes, &errorResponse)
if err != nil {
alternativeErrorResponse := alternativeErrorMessage{}
err = json.Unmarshal(bytes, &alternativeErrorResponse)
if err == nil {
alternativeErrorResponse.setErrorResponse(&errorResponse)
}
}
if err != nil {
message = string(bytes)
err = commonerrors.WrapError(commonerrors.ErrMarshalling, err, "error occurred when unmarshalling response")
return
}
apiErrorMessage := strings.Builder{}
apiErrorMessage.WriteString("API call error")
if code, _ := errorResponse.GetHttpStatusCodeOk(); !reflection.IsEmpty(code) {
apiErrorMessage.WriteString(fmt.Sprintf(" (%v)", *code))
}
apiErrorMessage.WriteString(fmt.Sprintf(" [request-id: %v] ", errorResponse.GetRequestId()))
apiErrorMessage.WriteString(errorResponse.GetMessage())
if fields, has := errorResponse.GetFieldsOk(); has {
apiErrorMessage.WriteString(" [")
start := true
for i := range fields {
subErr := parallelisation.DetermineContextError(ctx)
if subErr != nil {
err = subErr
return
}
if !start {
apiErrorMessage.WriteString(",")
}
field := fields[i]
apiErrorMessage.WriteString(fmt.Sprintf("%v: %v (%v)", field.GetFieldName(), field.GetMessage(), field.GetFieldPath()))
start = false
}
apiErrorMessage.WriteString("]")
}
message = apiErrorMessage.String()
return
}
type alternativeErrorMessage struct {
Fields []client.FieldObject `json:"fields,omitempty"`
HTTPStatusCode *int32 `json:"httpStatusCode,omitempty"`
Message string `json:"message"`
RequestID string `json:"requestId"`
}
func (e *alternativeErrorMessage) setErrorResponse(err *client.ErrorResponse) {
err.SetMessage(e.Message)
err.SetRequestId(e.RequestID)
err.SetFields(e.Fields)
if e.HTTPStatusCode != nil {
err.SetHttpStatusCode(*e.HTTPStatusCode)
}
}