first compiling version
This commit is contained in:
parent
60fb40d61a
commit
3d54199faa
27 changed files with 908 additions and 243 deletions
|
|
@ -8,8 +8,8 @@ import (
|
|||
)
|
||||
|
||||
type Error struct {
|
||||
Err string `json:"error"`
|
||||
Status int `json:"status"`
|
||||
Err string `json:"error"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
|
|
@ -20,7 +20,18 @@ func (e Error) Is(target error) bool {
|
|||
return e.Err == target.Error()
|
||||
}
|
||||
|
||||
func decodeResp(resp *http.Response, v interface{}) error {
|
||||
type Redirect struct {
|
||||
Location string
|
||||
Code int
|
||||
}
|
||||
|
||||
func (r Redirect) Error() string {
|
||||
return "redirect: " + r.Location
|
||||
}
|
||||
|
||||
var ErrRedirect = Redirect{}
|
||||
|
||||
func decodeResp(resp *http.Response, v any) error {
|
||||
if resp.Body == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -31,12 +42,16 @@ func decodeResp(resp *http.Response, v interface{}) error {
|
|||
}
|
||||
return json.NewDecoder(resp.Body).Decode(&v)
|
||||
}
|
||||
if resp.StatusCode >= 300 && resp.StatusCode < 400 {
|
||||
loc := resp.Header.Get("Location")
|
||||
return Redirect{loc, resp.StatusCode}
|
||||
}
|
||||
var e Error
|
||||
err := json.NewDecoder(resp.Body).Decode(&e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
|
||||
if resp.StatusCode >= 400 && resp.StatusCode < 600 {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue