Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Better reporting of REST errors. #443

Merged
merged 4 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 58 additions & 53 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 @@ -118,18 +118,18 @@ func (r *Client) Get(path string, object interface{}, params ...Param) (err erro
}
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
defer func() {
_ = reply.Body.Close()
_ = response.Body.Close()
}()
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusOK:
var body []byte
body, err = io.ReadAll(reply.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -139,10 +139,8 @@ func (r *Client) Get(path string, object interface{}, params ...Param) (err erro
err = liberr.Wrap(err)
return
}
case http.StatusNotFound:
err = &NotFound{Path: path}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}

return
Expand All @@ -167,16 +165,16 @@ func (r *Client) Post(path string, object interface{}) (err error) {
request.Header.Set(api.Accept, binding.MIMEJSON)
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusOK,
http.StatusCreated:
var body []byte
body, err = io.ReadAll(reply.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -187,10 +185,8 @@ func (r *Client) Post(path string, object interface{}) (err error) {
return
}
case http.StatusNoContent:
case http.StatusConflict:
err = &Conflict{Path: path}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}
return
}
Expand Down Expand Up @@ -221,17 +217,17 @@ func (r *Client) Put(path string, object interface{}, params ...Param) (err erro
}
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusNoContent:
case http.StatusOK,
http.StatusCreated:
var body []byte
body, err = io.ReadAll(reply.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -241,10 +237,8 @@ func (r *Client) Put(path string, object interface{}, params ...Param) (err erro
err = liberr.Wrap(err)
return
}
case http.StatusNotFound:
err = &NotFound{Path: path}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}

return
Expand All @@ -269,21 +263,19 @@ func (r *Client) Delete(path string, params ...Param) (err error) {
}
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
defer func() {
_ = reply.Body.Close()
_ = response.Body.Close()
}()
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusOK,
http.StatusNoContent:
case http.StatusNotFound:
err = &NotFound{Path: path}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}

return
Expand All @@ -302,27 +294,25 @@ func (r *Client) BucketGet(source, destination string) (err error) {
request.Header.Set(api.Accept, api.MIMEOCTETSTREAM)
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
defer func() {
_ = reply.Body.Close()
_ = response.Body.Close()
}()
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusNoContent:
// Empty.
case http.StatusOK:
if reply.Header.Get(api.Directory) == api.DirectoryExpand {
err = r.getDir(reply.Body, destination)
if response.Header.Get(api.Directory) == api.DirectoryExpand {
err = r.getDir(response.Body, destination)
} else {
err = r.getFile(reply.Body, source, destination)
err = r.getFile(response.Body, source, destination)
}
case http.StatusNotFound:
err = &NotFound{Path: source}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}
return
}
Expand Down Expand Up @@ -372,20 +362,18 @@ func (r *Client) BucketPut(source, destination string) (err error) {
}()
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusOK,
http.StatusNoContent,
http.StatusCreated,
http.StatusAccepted:
case http.StatusNotFound:
err = &NotFound{Path: destination}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}
return
}
Expand All @@ -402,23 +390,21 @@ func (r *Client) FileGet(path, destination string) (err error) {
request.Header.Set(api.Accept, api.MIMEOCTETSTREAM)
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
defer func() {
_ = reply.Body.Close()
_ = response.Body.Close()
}()
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusNoContent:
// Empty.
case http.StatusOK:
err = r.getFile(reply.Body, "", destination)
case http.StatusNotFound:
err = &NotFound{Path: path}
err = r.getFile(response.Body, "", destination)
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}
return
}
Expand Down Expand Up @@ -510,16 +496,16 @@ func (r *Client) FileSend(path, method string, fields []Field, object interface{
}()
return
}
reply, err := r.send(request)
response, err := r.send(request)
if err != nil {
return
}
status := reply.StatusCode
status := response.StatusCode
switch status {
case http.StatusOK,
http.StatusCreated:
var body []byte
body, err = io.ReadAll(reply.Body)
body, err = io.ReadAll(response.Body)
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -529,10 +515,8 @@ func (r *Client) FileSend(path, method string, fields []Field, object interface{
err = liberr.Wrap(err)
return
}
case http.StatusConflict:
err = &Conflict{Path: path}
default:
err = liberr.New(http.StatusText(status))
err = r.restError(response)
}
return
}
Expand Down Expand Up @@ -781,6 +765,27 @@ func (r *Client) join(path string) (parsedURL *url.URL) {
return
}

//
// restError returns an error based on status.
func (r *Client) restError(response *http.Response) (err error) {
status := response.StatusCode
if status < 400 {
return
}
switch status {
case http.StatusConflict:
restError := &Conflict{}
err = restError.With(response)
case http.StatusNotFound:
restError := &NotFound{}
err = restError.With(response)
default:
restError := &RestError{}
err = restError.With(response)
}
return
}

//
// Field file upload form field.
type Field struct {
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