Documentation
¶
Index ¶
- func Check(resp *http.Response, err error) (*http.Response, error)
- func CheckResponse(resp *http.Response, checker ResponseChecker) error
- func CheckStatus(resp *http.Response) error
- func CheckStatusOneOf(resp *http.Response, expectedStatuses ...int) error
- func DefaultResponseChecker(resp *http.Response) bool
- type ResponseChecker
- type StatusError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check is a convenience wrapper over CheckStatus -- if the passed error is non-nil, it is returned; otherwise, CheckStatus(resp) is returned.
This function exists to make it easier to write error handling code, by directly taking the expression that makes the request. For instance:
Check(http.Get("http://example.com"))
Check(http.Do(request))
Example ¶
package main
import (
"fmt"
"net/http"
"snai.pe/go-hterrors"
)
func main() {
resp, err := hterrors.Check(http.Get("http://google.com"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(http.StatusText(resp.StatusCode))
}
Output: OK
Example (Notfound) ¶
package main
import (
"fmt"
"net/http"
"snai.pe/go-hterrors"
)
func main() {
resp, err := hterrors.Check(http.Get("http://google.com/invalid"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(http.StatusText(resp.StatusCode))
}
Output: GET "http://google.com/invalid": Error 404 (Not Found)!!1 : 404. That’s an error. : The requested URL /invalid was not found on this server. That’s all we know.
func CheckResponse ¶
func CheckResponse(resp *http.Response, checker ResponseChecker) error
CheckResponse returns an error if the response is rejected by the specified response checker. The returned error contains a digested version of the response body, and the response body is consumed.
func CheckStatus ¶
CheckStatus returns an error if the status code of the specified response is not in the 2xx family. The returned error contains a digested version of the response body, and the reponse body is consumed.
It is a convenience wrapper over CheckResponse(resp, DefaultResponseChecker).
func CheckStatusOneOf ¶
CheckStatusOneOf returns an error if the status code of the specified response is not one of the expected statuses passed to this function. The returned error contains a digested version of the response body, and the reponse body is consumed.
func DefaultResponseChecker ¶
DefaultResponseChecker is the default ResponseChecker. It returns true if the status is 2xx.
Types ¶
type ResponseChecker ¶
ResponseChecker represents a function that accepts or rejects a response based off some criteria. If rejected, the response is considered failed by the CheckReponse function below and and error is returned.
type StatusError ¶
StatusError represents a non-2xx HTTP status code, and the associated message returned by the server, if any.
func (*StatusError) Error ¶
func (err *StatusError) Error() string