This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherror.go
88 lines (70 loc) · 1.93 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
87
88
package schemaregistry
import (
"encoding/json"
"fmt"
"net/http"
)
// These numbers are used by the schema registry to communicate errors.
const (
subjectNotFoundCode = 40401
versionNotFoundCode = 40402
schemaNotFoundCode = 40403
)
// ResourceError is being fired from all API calls when an error code is received.
type ResourceError struct {
ErrorCode int `json:"error_code"`
Method string `json:"method,omitempty"`
URI string `json:"uri,omitempty"`
Message string `json:"message,omitempty"`
}
// Error is used to implement the error interface.
func (err ResourceError) Error() string {
return fmt.Sprintf("client: (%s: %s) failed with error code %d: %s",
err.Method, err.URI, err.ErrorCode, err.Message)
}
// IsSubjectNotFound checks the returned error to see if it is kind of a subject
// not found error code.
func IsSubjectNotFound(err error) bool {
if err == nil {
return false
}
if resErr, ok := err.(ResourceError); ok {
return resErr.ErrorCode == subjectNotFoundCode
}
return false
}
// IsVersionNotFound checks the returned error to see if it's related to a
// version not found.
func IsVersionNotFound(err error) bool {
if err == nil {
return false
}
if resErr, ok := err.(ResourceError); ok {
return resErr.ErrorCode == versionNotFoundCode
}
return false
}
// IsSchemaNotFound checks the returned error to see if it is kind of a schema
// not found error code.
func IsSchemaNotFound(err error) bool {
if err == nil {
return false
}
if resErr, ok := err.(ResourceError); ok {
return resErr.ErrorCode == schemaNotFoundCode
}
return false
}
func parseResponseError(req *http.Request, res *http.Response) error {
if res.StatusCode == 200 {
return nil
}
var resErr ResourceError
err := json.NewDecoder(res.Body).Decode(&resErr)
if err != nil {
return fmt.Errorf("failed to decode the response: %s", err)
}
resErr.URI = req.URL.String()
resErr.Method = req.Method
return resErr
}