Skip to content

Commit

Permalink
Pass resp on retryable status (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Karn authored May 9, 2023
1 parent 9fe94b7 commit 6148921
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.17.1] - 2023-05-09
### Fixed
- ErrRetryableStatusCode passing the *http.Response to have access to not only the status code but headers etc. related to retrying.
- Added ErrUnexpectedResponse to pass back when encountering an unexpected response code to allow the caller to decide what to do.

## [5.17.0] - 2023-05-08
### Added
- bytesext.Bytes alias to int64 for better code clarity.
Expand Down Expand Up @@ -37,7 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added `timext.NanoTime` for fast low level monotonic time with nanosecond precision.

[Unreleased]: https://github.com/go-playground/pkg/compare/v5.17.0...HEAD
[Unreleased]: https://github.com/go-playground/pkg/compare/v5.17.1...HEAD
[5.17.1]: https://github.com/go-playground/pkg/compare/v5.17.0...v5.17.1
[5.17.0]: https://github.com/go-playground/pkg/compare/v5.16.0...v5.17.0
[5.16.0]: https://github.com/go-playground/pkg/compare/v5.15.2...v5.16.0
[5.15.2]: https://github.com/go-playground/pkg/compare/v5.15.1...v5.15.2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# pkg

![Project status](https://img.shields.io/badge/version-5.17.0-green.svg)
![Project status](https://img.shields.io/badge/version-5.17.1-green.svg)
[![Lint & Test](https://github.com/go-playground/pkg/actions/workflows/go.yml/badge.svg)](https://github.com/go-playground/pkg/actions/workflows/go.yml)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/pkg/badge.svg?branch=master)](https://coveralls.io/github/go-playground/pkg?branch=master)
[![GoDoc](https://godoc.org/github.com/go-playground/pkg?status.svg)](https://pkg.go.dev/mod/github.com/go-playground/pkg/v5)
Expand Down
21 changes: 13 additions & 8 deletions net/http/retryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"fmt"
bytesext "github.com/go-playground/pkg/v5/bytes"
errorsext "github.com/go-playground/pkg/v5/errors"
ioext "github.com/go-playground/pkg/v5/io"
resultext "github.com/go-playground/pkg/v5/values/result"
"io"
"net/http"
"strconv"
)
Expand All @@ -33,11 +31,20 @@ var (

// ErrRetryableStatusCode can be used to indicate a retryable HTTP status code was encountered as an error.
type ErrRetryableStatusCode struct {
StatusCode int
Response *http.Response
}

func (e ErrRetryableStatusCode) Error() string {
return fmt.Sprintf("retryable HTTP status code encountered: %d", e.StatusCode)
return fmt.Sprintf("retryable HTTP status code encountered: %d", e.Response.StatusCode)
}

// ErrUnexpectedResponse can be used to indicate an unexpected response was encountered as an error and provide access to the *http.Response.
type ErrUnexpectedResponse struct {
Response *http.Response
}

func (e ErrUnexpectedResponse) Error() string {
return "unexpected response encountered"
}

// IsRetryableStatusCode returns if the provided status code is considered retryable.
Expand Down Expand Up @@ -77,7 +84,7 @@ func DoRetryableResponse(ctx context.Context, onRetryFn errorsext.OnRetryFn[erro
}

if isRetryableStatusCode(resp.StatusCode) {
opt := onRetryFn(ctx, ErrRetryableStatusCode{StatusCode: resp.StatusCode}, strconv.Itoa(resp.StatusCode), attempt)
opt := onRetryFn(ctx, ErrRetryableStatusCode{Response: resp}, strconv.Itoa(resp.StatusCode), attempt)
if opt.IsSome() {
return resultext.Err[*http.Response, error](opt.Unwrap())
}
Expand Down Expand Up @@ -106,9 +113,7 @@ func DoRetryable[T any](ctx context.Context, isRetryableFn errorsext.IsRetryable
defer resp.Body.Close()

if resp.StatusCode != expectedResponseCode {
b, _ := io.ReadAll(ioext.LimitReader(resp.Body, maxMemory))
err := fmt.Errorf("invalid response status code: %d body: %s", resp.StatusCode, string(b))
return resultext.Err[T, error](err)
return resultext.Err[T, error](ErrUnexpectedResponse{Response: resp})
}

data, err := DecodeResponse[T](resp, maxMemory)
Expand Down

0 comments on commit 6148921

Please sign in to comment.