diff --git a/libauth/messages.go b/libauth/messages.go index e0d9093..b443a64 100644 --- a/libauth/messages.go +++ b/libauth/messages.go @@ -1,6 +1,20 @@ package libauth -var PortalError = map[string]string{ +import "fmt" + +type PortalError struct { + Code string + Message string +} + +func (e *PortalError) Error() string { + if e.Code != "" { + return fmt.Sprintf("%s: %s", e.Code, e.Message) + } + return e.Message +} + +var portalErrorMessages = map[string]string{ "E3001": "流量或时长已用尽", "E3002": "计费策略条件不匹配", "E3003": "控制策略条件不匹配", diff --git a/libauth/requests.go b/libauth/requests.go index a20d6e5..01cdae8 100644 --- a/libauth/requests.go +++ b/libauth/requests.go @@ -261,10 +261,10 @@ func LoginLogout(username, password string, host *UrlProvider, logout bool, anot err = nil } else { ecode, _ := loginResp["ecode"].(string) - if strerr, exist := PortalError[ecode]; exist { - err = errors.New(strerr) + if msg, exist := portalErrorMessages[ecode]; exist { + err = &PortalError{Code: ecode, Message: msg} } else { - err = errors.New(res) + err = &PortalError{Code: ecode, Message: res} } }