添加文件: ymt_v3/errors.go
This commit is contained in:
parent
2e74866bbe
commit
2be0b1f979
|
|
@ -0,0 +1,50 @@
|
|||
package ymt_v3
|
||||
|
||||
import "fmt"
|
||||
|
||||
// APIError 表示API返回的业务错误
|
||||
type APIError struct {
|
||||
Code int32 `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
return fmt.Sprintf("API error: code=%d, message=%s, reason=%s", e.Code, e.Message, e.Reason)
|
||||
}
|
||||
|
||||
// IsSuccess 判断是否成功
|
||||
func (e *APIError) IsSuccess() bool {
|
||||
return e.Code == 200
|
||||
}
|
||||
|
||||
// NewAPIError 创建API错误
|
||||
func NewAPIError(code int32, message, reason string) *APIError {
|
||||
return &APIError{
|
||||
Code: code,
|
||||
Message: message,
|
||||
Reason: reason,
|
||||
}
|
||||
}
|
||||
|
||||
// SDKError SDK内部错误
|
||||
type SDKError struct {
|
||||
Message string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (e *SDKError) Error() string {
|
||||
if e.Err != nil {
|
||||
return fmt.Sprintf("SDK error: %s: %v", e.Message, e.Err)
|
||||
}
|
||||
return fmt.Sprintf("SDK error: %s", e.Message)
|
||||
}
|
||||
|
||||
func (e *SDKError) Unwrap() error {
|
||||
return e.Err
|
||||
}
|
||||
|
||||
// NewSDKError 创建SDK内部错误
|
||||
func NewSDKError(msg string, err error) *SDKError {
|
||||
return &SDKError{Message: msg, Err: err}
|
||||
}
|
||||
Loading…
Reference in New Issue