httperror

package
v0.0.0-...-df7ef16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPError

type HTTPError struct {
	*http.Response
	// contains filtered or unexported fields
}

HTTPError represents a HTTP error document as a Go error and implements net.Error

Example
l, err := net.Listen("tcp", "127.0.0.1:0")
go (&http.Server{}).Serve(l)
defer l.Close()

// get returns an error for non-200 responses.
get := func(url string) (*http.Response, error) {
	resp, err := http.Get(url)
	if err == nil && resp.StatusCode != http.StatusOK {
		err = New(resp) // closes original resp.Body
	}
	return resp, err
}

url := fmt.Sprintf("http://%s/404", l.Addr())
resp, err := get(url)
for i := 0; i < 3; i++ {
	// retry after temporary network/http errors.
	if neterr, ok := err.(net.Error); ok && neterr.Temporary() {
		resp, err = get(url)
	} else {
		break
	}
}
if err != nil {
	fmt.Println(err)
	return
}
defer resp.Body.Close()
// use response
Output:

http error: 404 Not Found: 404 page not found

func New

func New(resp *http.Response) *HTTPError

New wraps a Response in an HTTPError. The response's Body is replaced by a small buffer so reading and closing it is purely optional.

func (*HTTPError) Error

func (he *HTTPError) Error() string

func (*HTTPError) Temporary

func (he *HTTPError) Temporary() bool

Temporary returns true for potentially temporary status codes such as server and gateway errors.

func (*HTTPError) Timeout

func (he *HTTPError) Timeout() bool

Timeout returns true for timeout status codes.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL