-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
45 lines (37 loc) · 1.08 KB
/
errors.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
package akamai
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// GetMessageFromErrorResponse gets the `message` JSON field from the given response body.
func GetMessageFromErrorResponse(body []byte) string {
type ErrorResponse struct {
Message string `json:"message"`
}
var response ErrorResponse
if err := json.Unmarshal(body, &response); err != nil {
return ""
}
return response.Message
}
// ApiOperationError represents a generic API request failure due to a bad HTTP status code.
type ApiOperationError struct {
// StatusCode is the HTTP response status code that caused the error.
StatusCode int
// Message is the error message provided by the server.
Message string
}
func (err ApiOperationError) Error() string {
var builder strings.Builder
builder.WriteString("akamai-sdk-go: API operation failed with HTTP ")
builder.WriteString(strconv.Itoa(err.StatusCode))
builder.WriteString(" ")
builder.WriteString(http.StatusText(err.StatusCode))
if err.Message != "" {
builder.WriteString("; ")
builder.WriteString(err.Message)
}
return builder.String()
}