Skip to content

Latest commit

 

History

History
101 lines (82 loc) · 2.11 KB

README.md

File metadata and controls

101 lines (82 loc) · 2.11 KB

errorutil Travis-CI GoDoc

go get github.com/objenious/errorutil

Delayed errors

func foo() error {
	err := bar()
	timer := time.Hour
	if err != nil {
		// it should be delayed 1h later
		return errorutil.WithDelay(err, timer) 
	}
	return bar() // will not be delayed
}

func main() {
	err := foo()
	time.AfterFunc(errorutil.Delay(err), func() {
		...
	})
}

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/objenious/errors :

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