-
Notifications
You must be signed in to change notification settings - Fork 12
/
error.go
158 lines (122 loc) · 4.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package driver
import (
"strconv"
"strings"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
)
// cookie err
var (
ErrBadCookie = errors.New("bad cookie")
)
var (
ErrNotLogin = errors.New("user not login")
ErrOfflineInvalidLink = errors.New("invalid download link")
ErrOfflineTaskExisted = errors.New("offline task existed")
ErrOrderNotSupport = errors.New("file order not supported")
ErrPasswordIncorrect = errors.New("password incorrect")
ErrLoginTwoStepVerify = errors.New("requires two-step verification")
ErrAccountNotBindMobile = errors.New("account not binds mobile")
ErrCredentialInvalid = errors.New("credential invalid")
ErrSessionExited = errors.New("session exited")
ErrQrcodeExpired = errors.New("qrcode expired")
// ErrUnexpected is the fall-back error whose code is not handled.
ErrUnexpected = errors.New("unexpected error")
// ErrExist means an item which you want to create is already existed.
ErrExist = errors.New("target already exists")
// ErrNotExist means an item which you find is not existed.
ErrNotExist = errors.New("target does not exist")
ErrInvalidCursor = errors.New("invalid cursor")
ErrUploadTooLarge = errors.New("upload reach the limit")
ErrUploadFailed = errors.New("upload failed")
ErrImportDirectory = errors.New("can not import directory")
ErrDownloadEmpty = errors.New("can not get download URL")
ErrDownloadDirectory = errors.New("can not download directory")
ErrDownloadFileNotExistOrHasDeleted = errors.New("target file does not exist or has deleted")
ErrDownloadFileTooBig = errors.New("target file is too big to download")
ErrCyclicCopy = errors.New("cyclic copy")
ErrCyclicMove = errors.New("cyclic move")
ErrVideoNotReady = errors.New("video is not ready")
ErrWrongParams = errors.New("wrong parameters")
ErrRepeatLogin = errors.New("repeat login")
ErrFailedToLogin = errors.New("failed to login")
ErrDoesLoggedOut = errors.New("you have been kicked out by multi-device login management")
ErrPickCodeNotExist = errors.New("pickcode does not exist")
ErrSharedInvalid = errors.New("shared link invalid")
ErrSharedNotFound = errors.New("shared link not found")
ErrPickCodeIsEmpty = errors.New("empty pickcode")
ErrUploadSH1Invalid = errors.New("userid/filesize/target/pickcode/ invalid")
ErrUploadSigInvalid = errors.New("sig invalid")
errMap = map[int]error{
// Normal errors
99: ErrNotLogin,
990001: ErrNotLogin,
// Offline errors
10004: ErrOfflineInvalidLink,
10008: ErrOfflineTaskExisted,
// Dir errors
20004: ErrExist,
// Label errors
21003: ErrExist,
// File errors
20130827: ErrOrderNotSupport,
50028: ErrDownloadFileTooBig,
70005: ErrDownloadFileNotExistOrHasDeleted,
231011: ErrDownloadFileNotExistOrHasDeleted,
91002: ErrCyclicCopy,
800006: ErrCyclicMove,
// Login errors
40101009: ErrPasswordIncorrect,
40101010: ErrLoginTwoStepVerify,
40101017: ErrFailedToLogin,
40100000: ErrWrongParams,
40101030: ErrAccountNotBindMobile,
40101032: ErrCredentialInvalid,
40101035: ErrDoesLoggedOut,
40101037: ErrSessionExited,
40101038: ErrRepeatLogin,
// QRCode errors
40199002: ErrQrcodeExpired,
// Params errors
1001: ErrWrongParams,
200900: ErrWrongParams,
990002: ErrWrongParams,
// share
4100009: ErrSharedInvalid,
4100026: ErrSharedNotFound,
// pickCode
50003: ErrPickCodeNotExist,
50001: ErrPickCodeIsEmpty,
// upload SH1
402: ErrUploadSH1Invalid,
400: ErrUploadSigInvalid,
}
)
func GetErr(code int, respBody ...string) error {
errWithMsg := ErrUnexpected
if err, found := errMap[code]; found {
errWithMsg = err
}
// if len(respBody) > 0 && errors.Is(ErrUnexpected, errWithMsg) {
if len(respBody) > 0 {
bodyRaw := respBody[0]
readableBody, err := strconv.Unquote(strings.Replace(strconv.Quote(bodyRaw), `\\u`, `\u`, -1))
if err != nil {
return errors.Wrap(errWithMsg, bodyRaw)
}
return errors.Wrap(errWithMsg, readableBody)
}
return errWithMsg
}
type ResultWithErr interface {
Err(respBody ...string) error
}
func CheckErr(err error, result ResultWithErr, restyResp *resty.Response) error {
if err == nil {
err = result.Err(restyResp.String())
}
if err != nil {
return err
}
return nil
}