From 87668d9bb73e2d3655a6bb6d4127f86f51cbc066 Mon Sep 17 00:00:00 2001 From: Peter Engelbert <36644727+pmengelbert@users.noreply.github.com> Date: Wed, 29 Jan 2020 15:52:36 -0600 Subject: [PATCH] Add error case for Error JSON parsing (#21) --- error.go | 12 +++++++----- response.go | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/error.go b/error.go index 801f099..777069a 100644 --- a/error.go +++ b/error.go @@ -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"` } ) diff --git a/response.go b/response.go index 6558a4e..5f41c25 100644 --- a/response.go +++ b/response.go @@ -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 ( @@ -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 }