github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/baidupcs/pcserror/pcserrorinfo.go (about) 1 package pcserror 2 3 import ( 4 "fmt" 5 ) 6 7 type ( 8 // PCSErrInfo PCS错误信息 9 PCSErrInfo struct { 10 Operation string // 正在进行的操作 11 ErrType ErrType 12 Err error 13 ErrCode int `json:"error_code"` // 错误代码 14 ErrMsg string `json:"error_msg"` // 错误消息 15 } 16 ) 17 18 // NewPCSErrorInfo 提供operation操作名称, 返回 *PCSErrInfo 19 func NewPCSErrorInfo(operation string) *PCSErrInfo { 20 return &PCSErrInfo{ 21 Operation: operation, 22 ErrType: ErrorTypeNoError, 23 } 24 } 25 26 // SetJSONError 设置JSON错误 27 func (pcse *PCSErrInfo) SetJSONError(err error) { 28 pcse.ErrType = ErrTypeJSONParseError 29 pcse.Err = err 30 } 31 32 // SetNetError 设置网络错误 33 func (pcse *PCSErrInfo) SetNetError(err error) { 34 pcse.ErrType = ErrTypeNetError 35 pcse.Err = err 36 } 37 38 // SetRemoteError 设置远端服务器错误 39 func (pcse *PCSErrInfo) SetRemoteError() { 40 pcse.ErrType = ErrTypeRemoteError 41 } 42 43 // GetOperation 获取操作 44 func (pcse *PCSErrInfo) GetOperation() string { 45 return pcse.Operation 46 } 47 48 // GetErrType 获取错误类型 49 func (pcse *PCSErrInfo) GetErrType() ErrType { 50 return pcse.ErrType 51 } 52 53 // GetRemoteErrCode 获取远端服务器错误代码 54 func (pcse *PCSErrInfo) GetRemoteErrCode() int { 55 return pcse.ErrCode 56 } 57 58 // GetRemoteErrMsg 获取远端服务器错误消息 59 func (pcse *PCSErrInfo) GetRemoteErrMsg() string { 60 _, msg := findPCSErr(pcse.ErrCode, pcse.ErrMsg) 61 return msg 62 } 63 64 // GetError 获取原始错误 65 func (pcse *PCSErrInfo) GetError() error { 66 return pcse.Err 67 } 68 69 func (pcse *PCSErrInfo) Error() string { 70 if pcse.Operation == "" { 71 if pcse.Err != nil { 72 return pcse.Err.Error() 73 } 74 return StrSuccess 75 } 76 77 switch pcse.ErrType { 78 case ErrTypeInternalError: 79 return fmt.Sprintf("%s: %s, %s", pcse.Operation, StrInternalError, pcse.Err) 80 case ErrTypeJSONParseError: 81 return fmt.Sprintf("%s: %s, %s", pcse.Operation, StrJSONParseError, pcse.Err) 82 case ErrTypeNetError: 83 return fmt.Sprintf("%s: %s, %s", pcse.Operation, StrNetError, pcse.Err) 84 case ErrTypeRemoteError: 85 if pcse.ErrCode == 0 { 86 return fmt.Sprintf("%s: %s", pcse.Operation, StrSuccess) 87 } 88 89 code, msg := findPCSErr(pcse.ErrCode, pcse.ErrMsg) 90 return fmt.Sprintf("%s: 遇到错误, %s, 代码: %d, 消息: %s", pcse.Operation, StrRemoteError, code, msg) 91 case ErrTypeOthers: 92 if pcse.Err == nil { 93 return fmt.Sprintf("%s: %s", pcse.Operation, StrSuccess) 94 } 95 96 return fmt.Sprintf("%s, 遇到错误, %s", pcse.Operation, pcse.Err) 97 default: 98 panic("pcserrorinfo: unknown ErrType") 99 } 100 } 101 102 // findPCSErr 检查 PCS 错误, 查找已知错误 103 func findPCSErr(errCode int, errMsg string) (int, string) { 104 switch errCode { 105 case 0: 106 return errCode, "" 107 case 31045: // user not exists 108 return errCode, "操作失败, 可能百度帐号登录状态过期, 请尝试重新登录, 消息: " + errMsg 109 case 31061: // file already exists 110 return errCode, "文件已存在" 111 case 31066: // file does not exist 112 return errCode, "文件或目录不存在" 113 case 31079: // file md5 not found, you should use upload api to upload the whole file. 114 return errCode, "秒传文件失败" 115 } 116 return errCode, errMsg 117 }