Skip to content

Commit

Permalink
Add error case for Error JSON parsing (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmengelbert authored Jan 29, 2020
1 parent 6fb5744 commit 87668d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package reggie

import (
spec "github.com/opencontainers/distribution-spec/specs-go/v1"
)

type (
ErrorResponse struct {
Errors []ErrorInfo `json:"errors"`
}

// ErrorInfo describes a server error returned from a registry.
ErrorInfo struct {
*spec.ErrorInfo
Code string `json:"code"`
Message string `json:"message"`
Detail interface{} `json:"detail"`
}
)
8 changes: 5 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package reggie

import (
"encoding/json"
"errors"
"net/http"
"net/url"

"github.com/go-resty/resty/v2"
spec "github.com/opencontainers/distribution-spec/specs-go/v1"
)

type (
Expand Down Expand Up @@ -46,15 +46,17 @@ func (resp *Response) IsUnauthorized() bool {

// Errors attempts to parse a response as OCI-compliant errors array
func (resp *Response) Errors() ([]ErrorInfo, error) {
errorResponse := &spec.ErrorResponse{}
errorResponse := &ErrorResponse{}
bodyBytes := []byte(resp.String())
err := json.Unmarshal(bodyBytes, errorResponse)
if err != nil {
return nil, err
} else if len(errorResponse.Errors) == 0 {
return nil, errors.New("body was valid json but could not be parsed")
}
errorList := []ErrorInfo{}
for _, errorInfo := range errorResponse.Errors {
errorList = append(errorList, ErrorInfo{&errorInfo})
errorList = append(errorList, errorInfo)
}
return errorList, nil
}

0 comments on commit 87668d9

Please sign in to comment.