support error.Is for ErrUnknownVersion (#615)

followup of https://gitea.com/gitea/go-sdk/pulls/612

see https://gitea.com/gitea/tea/pulls/538#issuecomment-734707

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/615
Reviewed-by: John Olheiser <john+gitea@jolheiser.com>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2023-04-03 11:34:49 +08:00 committed by Lunny Xiao
parent 7511c6d3cd
commit 6d1bcd107f
2 changed files with 8 additions and 2 deletions

View file

@ -68,7 +68,7 @@ func NewClient(url string, options ...ClientOption) (*Client, error) {
} }
} }
if err := client.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil { if err := client.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
if errors.As(err, &ErrUnknownVersion{}) { if errors.Is(err, &ErrUnknownVersion{}) {
return client, err return client, err
} }
return nil, err return nil, err

View file

@ -81,6 +81,12 @@ func (e ErrUnknownVersion) Error() string {
return fmt.Sprintf("unknown version: %s", e.raw) return fmt.Sprintf("unknown version: %s", e.raw)
} }
func (_ ErrUnknownVersion) Is(target error) bool {
_, ok1 := target.(*ErrUnknownVersion)
_, ok2 := target.(ErrUnknownVersion)
return ok1 || ok2
}
// checkServerVersionGreaterThanOrEqual is the canonical way in the SDK to check for versions for API compatibility reasons // checkServerVersionGreaterThanOrEqual is the canonical way in the SDK to check for versions for API compatibility reasons
func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error { func (c *Client) checkServerVersionGreaterThanOrEqual(v *version.Version) error {
if c.ignoreVersion { if c.ignoreVersion {
@ -111,7 +117,7 @@ func (c *Client) loadServerVersion() (err error) {
if strings.TrimSpace(raw) != "" { if strings.TrimSpace(raw) != "" {
// Version was something, just not recognized // Version was something, just not recognized
c.serverVersion = version1_11_0 c.serverVersion = version1_11_0
err = ErrUnknownVersion{raw: raw} err = &ErrUnknownVersion{raw: raw}
} }
return return
} }