diff --git a/ymt_v3/errors.go b/ymt_v3/errors.go new file mode 100644 index 0000000..64682d7 --- /dev/null +++ b/ymt_v3/errors.go @@ -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} +} \ No newline at end of file