Skip to content

Commit

Permalink
add typed error for failing to create hreq
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Jan 8, 2025
1 parent fe801a6 commit 8c676ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 0 additions & 4 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ type (
}
)

func newErrCreateHTTPRequest(err error) error {
return fmt.Errorf("failed to create http request: %w", err)
}

// HTTPStatus returns HTTP status or (-1) for non-HTTP error.
func HTTPStatus(err error) int {
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions api/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (args *PutArgs) getBody() (io.ReadCloser, error) { return args.Reader.Open(
func (args *PutArgs) put(reqArgs *cmn.HreqArgs) (*http.Request, error) {
req, err := reqArgs.Req()
if err != nil {
return nil, newErrCreateHTTPRequest(err)
return nil, cmn.NewErrCreateHreq(err)
}
// Go http doesn't automatically set this for files, so to handle redirect we do it here.
req.GetBody = args.getBody
Expand All @@ -273,7 +273,7 @@ func (args *PutArgs) put(reqArgs *cmn.HreqArgs) (*http.Request, error) {
if ckVal == "" {
_, ckhash, err := cos.CopyAndChecksum(io.Discard, args.Reader, nil, args.Cksum.Ty())
if err != nil {
return nil, newErrCreateHTTPRequest(err)
return nil, cmn.NewErrCreateHreq(err)
}
ckVal = hex.EncodeToString(ckhash.Sum())
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (args *AppendArgs) getBody() (io.ReadCloser, error) { return args.Reader.Op
func (args *AppendArgs) _append(reqArgs *cmn.HreqArgs) (*http.Request, error) {
req, err := reqArgs.Req()
if err != nil {
return nil, newErrCreateHTTPRequest(err)
return nil, cmn.NewErrCreateHreq(err)
}
// The HTTP package doesn't automatically set this for files, so it has to be done manually
// If it wasn't set, we would need to deal with the redirect manually.
Expand Down
15 changes: 15 additions & 0 deletions cmn/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ type (
ranges []string // RFC 7233
size int64 // [0, size)
}
ErrCreateHreq struct {
err error // original
}
)

var (
Expand Down Expand Up @@ -977,6 +980,18 @@ func IsErrRangeNotSatisfiable(err error) bool {
return ok
}

// ErrCreateHreq

func NewErrCreateHreq(err error) *ErrCreateHreq {
return &ErrCreateHreq{err}
}

func (e *ErrCreateHreq) Error() string {
return fmt.Sprintf("%v (cannot create http request)", e.err)
}

func (e *ErrCreateHreq) Unwrap() (err error) { return e.err }

//
// more is-error helpers
//
Expand Down

0 comments on commit 8c676ef

Please sign in to comment.