Skip to content

ltachet/errorutil

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

errorutil Travis-CI GoDoc

go get github.com/objenious/errorutil

Retryable errors

func foo() error {
  err := bar()
  if err != nil {
    // it should be retried
    return errorutil.RetryableError(err)
  }
  return baz() // will not be retried
}

func main() {
  err := foo()
  if errorutil.IsRetryable(err) {
    // retry !
  }
}

HTTP Aware errors

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")
if err != nil {
  // return error
}
defer resp.Body.Close()
if err := errorutil.HTTPError(resp); err != nil {
  // return error
}
// handle response

Find 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.StatusNotFound

Exponential backoff

backoffutil.Retry(func() error {
  resp, err := http.Get("http://www.example.com")
  if err != nil {
    return err
  }
  defer resp.Body.Close()
  if err := errorutil.HTTPError(resp); err != nil {
    return err
  }
  // Do something
  return nil
})

Notes

errorutil is compatible with https://github.com/pkg/errors :

err = errors.Wrap(errorutil.RetryableError(err), "some message")
errorutil.IsRetryable(err) // returns true

About

Backoff & HTTP-Aware Golang Errors

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%