-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
86 lines (75 loc) · 2.37 KB
/
error.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
// Package aiven provides a client for interacting with the Aiven API.
package aiven
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
)
// Error represents an Aiven API Error.
type Error struct {
// Aiven error response fields https://api.aiven.io/doc/#section/Responses/Failed-responses
Message string `json:"message"`
Errors any `json:"errors"`
// Internal fields
OperationID string `json:"-"`
Status int `json:"-"`
}
// Error concatenates all the fields.
func (e Error) Error() string {
msg := e.Message
if e.Errors != nil {
// Appends Errors if they don't contain the message
// Otherwise, it will be duplicated
b, err := json.Marshal(e.Errors)
errMsg := string(b)
if err != nil {
errMsg = err.Error()
}
if !strings.Contains(errMsg, msg) {
msg = fmt.Sprintf("%s (%s)", msg, errMsg)
}
}
// Must not use `%q` here which will escape every quote in the string,
// which might break external substring checks
return fmt.Sprintf(`[%d %s]: %s`, e.Status, e.OperationID, msg)
}
// IsNotFound returns true if the specified error has status 404
func IsNotFound(err error) bool {
var e Error
return errors.As(err, &e) && e.Status == http.StatusNotFound
}
// IsAlreadyExists returns true if the error message and error code that indicates that entity already exists
func IsAlreadyExists(err error) bool {
var e Error
return errors.As(err, &e) && strings.Contains(e.Message, "already exists") && e.Status == http.StatusConflict
}
func fromResponse(operationID string, rsp *http.Response) ([]byte, error) {
e := Error{OperationID: operationID, Status: rsp.StatusCode}
b, err := io.ReadAll(rsp.Body)
if err != nil {
e.Message = fmt.Sprintf("body read error: %s", err)
return nil, e
}
if rsp.StatusCode < 200 || rsp.StatusCode >= 300 {
// According to the documentation,
// failed responses must have "errors" and "message" fields
// https://api.aiven.io/doc/#section/Responses/Failed-responses
err = json.Unmarshal(b, &e)
if err != nil {
// 1. The body might contain sensitive data
// 2. It might fail the unmarshalling into Error and still be a valid json
if json.Valid(b) {
e.Message = err.Error()
} else {
// If it is not valid json, it shouldn't contain sensitive data
// Returns the body, because clients might relay on the error message
e.Message = string(b)
}
}
return nil, e
}
return b, nil
}