go get github.com/objenious/errorutil
funcfoo() error {
err:=bar()
timer:=time.Houriferr!=nil {
// it should be delayed 1h laterreturnerrorutil.WithDelay(err, timer) }
returnbar() // will not be delayed
}
funcmain() {
err:=foo()
time.AfterFunc(errorutil.Delay(err), func() {
...
})
}funcfoo() error {
err:=bar()
iferr!=nil {
// it should be retriedreturnerrorutil.RetryableError(err)
}
returnbaz() // will not be retried
}
funcmain() {
err:=foo()
iferrorutil.IsRetryable(err) {
// retry !
}
}Build an error based on a http.Response. Status code above 299, except 304, will be considered an error.
It will be retryable if status code is http.StatusBadGateway, http.StatusGatewayTimeout, http.StatusServiceUnavailable, http.StatusInternalServerError or 429 (Too many requests).
resp, err:=http.Get("http://www.example.com")
iferr!=nil {
// return error
}
deferresp.Body.Close()
iferr:=errorutil.HTTPError(resp); err!=nil {
// return error
}
// handle responseFind the most appropriate status code for an error :
w.WriteHeader(errorutil.HTTPStatusCode(err))Generate specific error types :
err:=errors.New("some error")
err=errorutil.NotFoundError(err)
w.WriteHeader(errorutil.HTTPStatusCode(err)) // returns http.StatusNotFoundbackoffutil.Retry(func() error {
resp, err:=http.Get("http://www.example.com")
iferr!=nil {
returnerr
}
deferresp.Body.Close()
iferr:=errorutil.HTTPError(resp); err!=nil {
returnerr
}
// Do somethingreturnnil
})errorutil is compatible with https://github.com/objenious/errors :
err=errors.Wrap(errorutil.RetryableError(err), "some message")
errorutil.IsRetryable(err) // returns true