-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiError.go
105 lines (84 loc) · 2.69 KB
/
apiError.go
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
102
103
104
105
// Package https Copyright (c) APIMatic. All rights reserved.
package https
import (
"fmt"
"net/http"
"regexp"
"strings"
)
// ApiError is the base struct for all error responses from the server.
// It holds information about the original HTTP request, the status code, headers, and response body.
type ApiError struct {
Request http.Request
StatusCode int
Headers http.Header
Body []byte
Message string
}
func (a ApiError) String() string {
return fmt.Sprintf(
"ApiError[StatusCode=%v, Message=%v]", a.StatusCode, a.Message)
}
func (a ApiError) Error() string {
return fmt.Sprintf("ApiError occurred: %v", a.Message)
}
type ErrorBuilder[T error] struct {
Message string
TemplatedMessage string
Unmarshaller func(ApiError) T
}
func (eb ErrorBuilder[T]) Build(httpctx HttpContext) error {
respStatusCode := httpctx.Response.StatusCode
respHeader := httpctx.Response.Header
respBodyBytes, _ := httpctx.GetResponseBody()
message := eb.Message
if eb.TemplatedMessage != "" {
message = renderErrorTemplate(eb.TemplatedMessage, respStatusCode, respHeader, respBodyBytes)
}
err := ApiError{
Request: *httpctx.Request,
StatusCode: respStatusCode,
Headers: respHeader,
Body: respBodyBytes,
Message: message,
}
if eb.Unmarshaller != nil {
return eb.Unmarshaller(err)
}
return err
}
func renderErrorTemplate(tpl string, respStatusCode int, respHeader http.Header, respBytes []byte) string {
placeholderRegex := `\{\$(.*?)\}`
re := regexp.MustCompile(placeholderRegex)
// Extract placeholders into an array of strings
placeholders := re.FindAllString(tpl, -1)
renderedVals := make([]any, 0)
for _, placeholder := range placeholders {
renderedVals = append(renderedVals, renderPlaceholder(placeholder, respStatusCode, respHeader, respBytes))
}
// Replace each instance of a placeholder with "%v"
formattedTpl := re.ReplaceAllString(tpl, "%v")
return fmt.Sprintf(formattedTpl, renderedVals...)
}
func renderPlaceholder(placeholder string, respStatusCode int, respHeader http.Header, respBytes []byte) any {
if placeholder == "{$statusCode}" {
return respStatusCode
}
if strings.HasPrefix(placeholder, "{$response.header.") {
headerName := placeholder[len("{$response.header.") : len(placeholder)-1]
return respHeader.Get(headerName)
}
// Return Response Body as-is
if placeholder == "{$response.body}" {
return string(respBytes)
}
// Use JSON Pointer to get the desired value from a JSON Response Body
if strings.HasPrefix(placeholder, "{$response.body#") {
jsonPtr := placeholder[len("{$response.body#") : len(placeholder)-1]
if jsonPtr == "" {
return ""
}
return getValueFromJSON(respBytes, jsonPtr)
}
return placeholder
}