github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/baidupcs/pcserror/xpanerrorinfo.go (about)

     1  package pcserror
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type (
     8  	// PanErrorInfo 网盘网页的api错误
     9  	XPanErrorInfo struct {
    10  		Operation string
    11  		ErrType   ErrType
    12  		Err       error
    13  		ErrNo     int `json:"errno"`
    14  		ReturnType int `json:"return_type"`
    15  	}
    16  )
    17  
    18  // NewXPanErrorInfo 提供operation操作名称, 返回 *PanErrorInfo
    19  func NewXPanErrorInfo(operation string) *XPanErrorInfo {
    20  	return &XPanErrorInfo{
    21  		Operation: operation,
    22  		ErrType:   ErrorTypeNoError,
    23  	}
    24  }
    25  
    26  // SetJSONError 设置JSON错误
    27  func (pane *XPanErrorInfo) SetJSONError(err error) {
    28  	pane.ErrType = ErrTypeJSONParseError
    29  	pane.Err = err
    30  }
    31  
    32  // SetNetError 设置网络错误
    33  func (pane *XPanErrorInfo) SetNetError(err error) {
    34  	pane.ErrType = ErrTypeNetError
    35  	pane.Err = err
    36  }
    37  
    38  // SetRemoteError 设置远端服务器错误
    39  func (pane *XPanErrorInfo) SetRemoteError() {
    40  	pane.ErrType = ErrTypeRemoteError
    41  }
    42  
    43  // GetOperation 获取操作
    44  func (pane *XPanErrorInfo) GetOperation() string {
    45  	return pane.Operation
    46  }
    47  
    48  // GetErrType 获取错误类型
    49  func (pane *XPanErrorInfo) GetErrType() ErrType {
    50  	return pane.ErrType
    51  }
    52  
    53  // GetRemoteErrCode 获取远端服务器错误代码
    54  func (pane *XPanErrorInfo) GetRemoteErrCode() int {
    55  	return pane.ErrNo + pane.ReturnType - 2
    56  }
    57  
    58  // GetRemoteErrMsg 获取远端服务器错误消息
    59  func (pane *XPanErrorInfo) GetRemoteErrMsg() string {
    60  	return FindXPanErr(pane.ErrNo, pane.ReturnType)
    61  }
    62  
    63  // GetError 获取原始错误
    64  func (pane *XPanErrorInfo) GetError() error {
    65  	return pane.Err
    66  }
    67  
    68  func (pane *XPanErrorInfo) Error() string {
    69  	if pane.Operation == "" {
    70  		if pane.Err != nil {
    71  			return pane.Err.Error()
    72  		}
    73  		return StrSuccess
    74  	}
    75  
    76  	switch pane.ErrType {
    77  	case ErrTypeInternalError:
    78  		return fmt.Sprintf("%s: %s, %s", pane.Operation, StrInternalError, pane.Err)
    79  	case ErrTypeJSONParseError:
    80  		return fmt.Sprintf("%s: %s, %s", pane.Operation, StrJSONParseError, pane.Err)
    81  	case ErrTypeNetError:
    82  		return fmt.Sprintf("%s: %s, %s", pane.Operation, StrNetError, pane.Err)
    83  	case ErrTypeRemoteError:
    84  		if pane.ErrNo == 0 && pane.ReturnType == 2 {
    85  			return fmt.Sprintf("%s: %s", pane.Operation, StrSuccess)
    86  		}
    87  
    88  		errmsg := FindXPanErr(pane.ErrNo, pane.ReturnType)
    89  		return fmt.Sprintf("%s: 遇到错误, %s, 代码: %d, 消息: %s", pane.Operation, StrRemoteError, pane.ErrNo, errmsg)
    90  	case ErrTypeOthers:
    91  		if pane.Err == nil {
    92  			return fmt.Sprintf("%s: %s", pane.Operation, StrSuccess)
    93  		}
    94  
    95  		return fmt.Sprintf("%s, 遇到错误, %s", pane.Operation, pane.Err)
    96  	default:
    97  		panic("xpanerrorinfo: unknown ErrType")
    98  	}
    99  }
   100  
   101  // FindPanErr 根据 ErrNo, 解析网盘错误信息
   102  func FindXPanErr(errno, returnType int) (errmsg string) {
   103  	switch errno {
   104  	case 0:
   105  		if returnType == 2 {
   106  			return StrSuccess
   107  		}
   108  		return fmt.Sprintf("错误类型: %d", returnType)
   109  	default:
   110  		return fmt.Sprintf("错误类型: %d", returnType)
   111  	}
   112  }