Skip to content

Commit

Permalink
🐛 Better reporting of error responses.
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel committed Jul 15, 2023
1 parent f22a7a4 commit 12b9ad1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
34 changes: 17 additions & 17 deletions binding/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
pathlib "path"
"path/filepath"
"strings"
"time"
"time"
)

const (
Expand Down Expand Up @@ -140,9 +140,9 @@ func (r *Client) Get(path string, object interface{}, params ...Param) (err erro
return
}
case http.StatusNotFound:
err = &NotFound{Path: path}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}

return
Expand Down Expand Up @@ -188,9 +188,9 @@ func (r *Client) Post(path string, object interface{}) (err error) {
}
case http.StatusNoContent:
case http.StatusConflict:
err = &Conflict{Path: path}
err = (&Conflict{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}
return
}
Expand Down Expand Up @@ -242,9 +242,9 @@ func (r *Client) Put(path string, object interface{}, params ...Param) (err erro
return
}
case http.StatusNotFound:
err = &NotFound{Path: path}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}

return
Expand Down Expand Up @@ -281,9 +281,9 @@ func (r *Client) Delete(path string, params ...Param) (err error) {
case http.StatusOK,
http.StatusNoContent:
case http.StatusNotFound:
err = &NotFound{Path: path}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}

return
Expand Down Expand Up @@ -320,9 +320,9 @@ func (r *Client) BucketGet(source, destination string) (err error) {
err = r.getFile(reply.Body, source, destination)
}
case http.StatusNotFound:
err = &NotFound{Path: source}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}
return
}
Expand Down Expand Up @@ -383,9 +383,9 @@ func (r *Client) BucketPut(source, destination string) (err error) {
http.StatusCreated,
http.StatusAccepted:
case http.StatusNotFound:
err = &NotFound{Path: destination}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}
return
}
Expand Down Expand Up @@ -416,9 +416,9 @@ func (r *Client) FileGet(path, destination string) (err error) {
case http.StatusOK:
err = r.getFile(reply.Body, "", destination)
case http.StatusNotFound:
err = &NotFound{Path: path}
err = (&NotFound{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}
return
}
Expand Down Expand Up @@ -530,9 +530,9 @@ func (r *Client) FileSend(path, method string, fields []Field, object interface{
return
}
case http.StatusConflict:
err = &Conflict{Path: path}
err = (&Conflict{}).With(reply)
default:
err = liberr.New(http.StatusText(status))
err = (&RestError{}).With(reply)
}
return
}
Expand Down
63 changes: 51 additions & 12 deletions binding/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package binding

import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
)

//
Expand All @@ -24,14 +27,55 @@ func (e *SoftError) Soft() *SoftError {
}

//
// Conflict reports 409 error.
type Conflict struct {
// RestError reports REST errors.
type RestError struct {
SoftError
Path string
Method string
Path string
Status int
Body string
}

func (e *RestError) Is(err error) (matched bool) {
_, matched = err.(*RestError)
return
}

func (e Conflict) Error() string {
return fmt.Sprintf("POST: path:%s (conflict)", e.Path)
func (e *RestError) Error() (s string) {
s = e.Reason
return
}

func (e *RestError) With(r *http.Response) *RestError {
e.Method = r.Request.Method
e.Path = r.Request.URL.Path
e.Status = r.StatusCode
if r.Body != nil {
body, err := io.ReadAll(r.Body)
if err == nil {
e.Body = string(body)
}
}
s := strings.ToUpper(e.Method)
s += " "
s += e.Path
s += " failed: "
s += strconv.Itoa(e.Status)
s += "("
s += http.StatusText(e.Status)
s += ")"
if e.Body != "" {
s += " body: "
s += e.Body
}
e.Reason = s
return e
}

//
// Conflict reports 409 error.
type Conflict struct {
RestError
}

func (e *Conflict) Is(err error) (matched bool) {
Expand All @@ -42,12 +86,7 @@ func (e *Conflict) Is(err error) (matched bool) {
//
// NotFound reports 404 error.
type NotFound struct {
SoftError
Path string
}

func (e NotFound) Error() string {
return fmt.Sprintf("HTTP path:%s (not-found)", e.Path)
RestError
}

func (e *NotFound) Is(err error) (matched bool) {
Expand Down

0 comments on commit 12b9ad1

Please sign in to comment.