-
Notifications
You must be signed in to change notification settings - Fork 310
/
pcserror.go
108 lines (94 loc) · 2.31 KB
/
pcserror.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
106
107
108
// Package pcserror PCS错误包
package pcserror
import (
"github.com/json-iterator/go"
"io"
)
type (
// ErrType 错误类型
ErrType int
// PCSErrInfo 错误信息
PCSErrInfo struct {
Operation string // 正在进行的操作
ErrType ErrType
Err error
ErrCode int `json:"error_code"` // 错误代码
ErrMsg string `json:"error_msg"` // 错误消息
}
// PanErrorInfo 网盘网页的api错误
PanErrorInfo struct {
Operation string
ErrType ErrType
Err error
ErrNo int `json:"errno"`
// ErrMsg string `json:"err_msg"`
}
Error interface {
error
SetJSONError(err error)
SetNetError(err error)
SetRemoteError()
GetOperation() string
GetErrType() ErrType
GetRemoteErrCode() int
GetRemoteErrMsg() string
GetError() error
}
)
const (
// ErrorTypeNoError 无错误
ErrorTypeNoError ErrType = iota
// ErrTypeInternalError 内部错误
ErrTypeInternalError
// ErrTypeRemoteError 远端服务器返回错误
ErrTypeRemoteError
// ErrTypeNetError 网络错误
ErrTypeNetError
// ErrTypeJSONParseError json 数据解析失败
ErrTypeJSONParseError
// ErrTypeOthers 其他错误
ErrTypeOthers
)
const (
// StrSuccess 操作成功
StrSuccess = "操作成功"
// StrInternalError 内部错误
StrInternalError = "内部错误"
// StrRemoteError 远端服务器返回错误
StrRemoteError = "远端服务器返回错误"
// StrNetError 网络错误
StrNetError = "网络错误"
// StrJSONParseError json 数据解析失败
StrJSONParseError = "json 数据解析失败"
)
// DecodePCSJSONError 解析PCS JSON的错误
func DecodePCSJSONError(opreation string, data io.Reader) Error {
errInfo := NewPCSErrorInfo(opreation)
return decodeJSONError(data, errInfo)
}
// DecodePanJSONError 解析Pan JSON的错误
func DecodePanJSONError(opreation string, data io.Reader) Error {
errInfo := NewPanErrorInfo(opreation)
return decodeJSONError(data, errInfo)
}
func decodeJSONError(data io.Reader, errInfo Error) Error {
var (
d = jsoniter.NewDecoder(data)
err error
)
switch value := errInfo.(type) {
case *PCSErrInfo:
err = d.Decode(value)
case *PanErrorInfo:
err = d.Decode(value)
}
if err != nil {
errInfo.SetJSONError(err)
return errInfo
}
if errInfo.GetRemoteErrCode() != 0 {
errInfo.SetRemoteError()
return errInfo
}
return nil
}