39 lines
802 B
Go
39 lines
802 B
Go
package sdk
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
)
|
||
|
||
// APIError 表示服务端返回的 HTTP 错误(write_error 格式)。
|
||
type APIError struct {
|
||
StatusCode int `json:"status_code"`
|
||
Message string `json:"error"`
|
||
}
|
||
|
||
func (e *APIError) Error() string {
|
||
if e == nil {
|
||
return "qmt api error"
|
||
}
|
||
if e.Message == "" {
|
||
return fmt.Sprintf("qmt api: http %d", e.StatusCode)
|
||
}
|
||
return fmt.Sprintf("qmt api: http %d: %s", e.StatusCode, e.Message)
|
||
}
|
||
|
||
func (e *APIError) Unauthorized() bool {
|
||
return e != nil && e.StatusCode == http.StatusUnauthorized
|
||
}
|
||
|
||
// BusinessError 表示 HTTP 200 但业务 JSON 带 error 字段。
|
||
type BusinessError struct {
|
||
Message string
|
||
}
|
||
|
||
func (e *BusinessError) Error() string {
|
||
if e == nil || e.Message == "" {
|
||
return "qmt api business error"
|
||
}
|
||
return e.Message
|
||
}
|