-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V2
- Loading branch information
Showing
7 changed files
with
385 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (C) 2019-2021, Xiongfa Li. | ||
// @author xiongfa.li | ||
// @version V1.0 | ||
// Description: | ||
|
||
package restclient | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
var DefaultErrorStatus = http.StatusBadRequest | ||
|
||
type Error interface { | ||
error | ||
|
||
// 获得http status code | ||
StatusCode() int | ||
|
||
// 获得原始error | ||
Origin() error | ||
} | ||
|
||
type defaultError struct { | ||
status int | ||
err error | ||
} | ||
|
||
func withErr(status int, err error) defaultError { | ||
return defaultError{ | ||
status: status, | ||
err: err, | ||
} | ||
} | ||
|
||
func withStatus(status int) defaultError { | ||
return defaultError{ | ||
status: status, | ||
err: fmt.Errorf("restclient status: [%d] %s", status, http.StatusText(status)), | ||
} | ||
} | ||
|
||
func (e defaultError) Origin() error { | ||
return e.err | ||
} | ||
|
||
func (e defaultError) Error() string { | ||
if e.err != nil { | ||
return e.err.Error() | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func (e defaultError) StatusCode() int { | ||
return e.status | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.