50 lines
1.0 KiB
Go
50 lines
1.0 KiB
Go
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}
|
|
} |