-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
49 lines (41 loc) · 1.21 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
package baidupcs
import (
"fmt"
)
const (
// StrRemoteError 远端服务器返回错误
StrRemoteError = "远端服务器返回错误"
)
// ErrInfo 远端服务器返回的错误信息
type ErrInfo struct {
Operation string `json:"-"` // 正在进行的操作
ErrCode int `json:"error_code"` // 错误代码
ErrMsg string `json:"error_msg"` // 错误消息
}
// NewErrorInfo 提供operation操作名称, 返回 *ErrInfo
func NewErrorInfo(operation string) *ErrInfo {
return &ErrInfo{
Operation: operation,
}
}
// FindErr 查找已知错误
func (e *ErrInfo) FindErr() (errCode int, errMsg string) {
return findErr(e.ErrCode, e.ErrMsg)
}
func (e *ErrInfo) Error() string {
if e.ErrCode == 0 {
return e.Operation + " 操作成功"
}
code, msg := e.FindErr()
return fmt.Sprintf("%s 遇到错误, %s, 代码: %d, 消息: %s", e.Operation, StrRemoteError, code, msg)
}
// findErr 检查 PCS 错误, 查找已知错误
func findErr(errCode int, errMsg string) (int, string) {
switch errCode {
case 0:
return errCode, ""
case 31045: // user not exists
return errCode, "操作失败, 可能百度帐号登录状态过期, 请尝试重新登录, 消息: " + errMsg
}
return errCode, errMsg
}