dev 1
This commit is contained in:
38
go-client/sdk/error.go
Normal file
38
go-client/sdk/error.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user