Ai
1 Star 0 Fork 0

ryancartoon/sensu-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
error.go 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
package client
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-resty/resty"
"github.com/sensu/sensu-go/backend/apid/actions"
)
// APIError describes an error message returned by the REST API
type APIError struct {
Message string `json:"message"`
Code uint32 `json:"code,omitempty"`
}
func (a APIError) Error() string {
return a.Message
}
// UnmarshalError decode the API error
// TODO: Export err type from routers package.
func UnmarshalError(res *resty.Response) error {
var apiErr APIError
switch res.StatusCode() {
case http.StatusPaymentRequired:
apiErr.Code = uint32(actions.PaymentRequired)
apiErr.Message = "This functionality requires a valid Sensu Go license. Please install a valid license file and restart or contact Sales for a trial."
default:
if err := json.Unmarshal(res.Body(), &apiErr); err != nil {
if len(res.Body()) > 0 {
apiErr.Message = string(res.Body())
} else {
apiErr.Message = fmt.Sprintf("the API returned: %s", res.Status())
}
}
}
return apiErr
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ryancartoon/sensu-go.git
git@gitee.com:ryancartoon/sensu-go.git
ryancartoon
sensu-go
sensu-go
v5.10.1

搜索帮助