-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
51 lines (41 loc) · 912 Bytes
/
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
package appstoreserverapi
import (
"encoding/json"
"errors"
"fmt"
)
// ApiError https://developer.apple.com/documentation/appstoreserverapi/error_codes
type ApiError struct {
Code int `json:"errorCode"`
Message string `json:"errorMessage"`
}
func (e ApiError) Error() string {
return fmt.Sprintf("errorCode:%d, errorMessage:%s", e.Code, e.Message)
}
func ParseApiError(data []byte) (*ApiError, error) {
var e ApiError
if err := json.Unmarshal(data, &e); err != nil {
return nil, err
}
return &e, nil
}
func ApiErrorFromError(err error) (*ApiError, bool) {
if err == nil {
return nil, true
}
var apiErr ApiError
if errors.As(err, &apiErr) {
return &apiErr, true
}
return nil, false
}
func handleApiErr(statusCode int, payload []byte) error {
if statusCode != 200 {
apiErr, err := ParseApiError(payload)
if err != nil {
return err
}
return apiErr
}
return nil
}