| 1 | package helpersscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/internal/helpers`/ |
| 2 | |
| 3 | import ( |
| 4 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/" |
| 5 | "encoding/xmlscip-go gomod github.com/golang/go/src go1.13 `encoding/xml`/" |
| 6 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 7 | "io/ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/" |
| 8 | "net/httpscip-go gomod github.com/golang/go/src go1.13 `net/http`/" |
| 9 | "stringsscip-go gomod github.com/golang/go/src go1.13 strings/" |
| 10 | |
| 11 | "github.com/Azure/go-autorest/autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/" |
| 12 | "github.com/Azure/go-autorest/autorest/azurescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/" |
| 13 | ) |
| 14 | |
| 15 | // TODO: trial switching over to this, then upstream it |
| 16 | |
| 17 | // WithErrorUnlessStatusCode returns a RespondDecorator that emits an |
| 18 | // azure.RequestError by reading the response body unless the response HTTP status code |
| 19 | // is among the set passed. |
| 20 | // |
| 21 | // If there is a chance service may return responses other than the Azure error |
| 22 | // format and the response cannot be parsed into an error, a decoding error will |
| 23 | // be returned containing the response body. In any case, the Responder will |
| 24 | // return an error if the status code is not satisfied. |
| 25 | // |
| 26 | // If this Responder returns an error, the response body will be replaced with |
| 27 | // an in-memory reader, which needs no further closing. |
| 28 | func WithErrorUnlessStatusCodescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/internal/helpers`/WithErrorUnlessStatusCode().(codes ...int) autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.RespondDecoratorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/RespondDecorator# { |
| 29 | return func(r autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.Responderscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/Responder#) autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.Responderscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/Responder# { |
| 30 | return autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.ResponderFuncscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/ResponderFunc#(func(resp *httpscip-go gomod github.com/golang/go/src go1.13 `net/http`/.Responsescip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#) error { |
| 31 | err := r.Respondscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/Responder#Respond().(resp) |
| 32 | if err == nil && !autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.ResponseHasStatusCodescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/ResponseHasStatusCode().(resp, codes...) { |
| 33 | var e azurescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/.RequestErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError# |
| 34 | defer resp.Bodyscip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#Body..Closescip-go gomod github.com/golang/go/src go1.13 io/Closer#Close().() |
| 35 | |
| 36 | contentType := autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsJSONscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsJSON. |
| 37 | if resp != nil { |
| 38 | contentTypeStr := resp.Headerscip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#Header..Getscip-go gomod github.com/golang/go/src go1.13 `net/http`/Header#Get().("Content-Type") |
| 39 | if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.EqualFoldscip-go gomod github.com/golang/go/src go1.13 strings/EqualFold().(contentTypeStr, "application/xml") { |
| 40 | contentType = autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsXMLscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsXML. |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Copy and replace the Body in case it does not contain an error object. |
| 45 | // This will leave the Body available to the caller. |
| 46 | b, decodeErr := autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.CopyAndDecodescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/CopyAndDecode().(contentType, resp.Bodyscip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#Body., &e) |
| 47 | resp.Bodyscip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#Body. = ioutilscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/.NopCloserscip-go gomod github.com/golang/go/src go1.13 `io/ioutil`/NopCloser().(&b) |
| 48 | if decodeErr != nil { |
| 49 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("autorest/azure: error response cannot be parsed: %q error: %v", b.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().(), decodeErr) |
| 50 | } |
| 51 | if e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError. == nil { |
| 52 | switch contentType { |
| 53 | case autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsJSONscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsJSON.: |
| 54 | // Check if error is unwrapped ServiceError |
| 55 | if err := jsonscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/Unmarshal().(b.Bytesscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#Bytes().(), &e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError.); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | break |
| 59 | case autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsXMLscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsXML.: |
| 60 | // Check if error is unwrapped ServiceError |
| 61 | if err := xmlscip-go gomod github.com/golang/go/src go1.13 `encoding/xml`/.Unmarshalscip-go gomod github.com/golang/go/src go1.13 `encoding/xml`/Unmarshal().(b.Bytesscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#Bytes().(), &e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError.); err != nil { |
| 62 | return err |
| 63 | } |
| 64 | break |
| 65 | } |
| 66 | } |
| 67 | if e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError..Messagescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ServiceError#Message. == "" { |
| 68 | |
| 69 | rawBody := map[string]interface{}{} |
| 70 | |
| 71 | switch contentType { |
| 72 | case autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsJSONscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsJSON.: |
| 73 | // if we're here it means the returned error wasn't OData v4 compliant. |
| 74 | // try to unmarshal the body as raw JSON in hopes of getting something. |
| 75 | if err := jsonscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.13 `encoding/json`/Unmarshal().(b.Bytesscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#Bytes().(), &rawBody); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | break |
| 79 | |
| 80 | case autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.EncodedAsXMLscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/EncodedAsXML.: |
| 81 | // if we're here it means the returned error wasn't OData v4 compliant. |
| 82 | // try to unmarshal the body as raw XML in hopes of getting something. |
| 83 | if err := xmlscip-go gomod github.com/golang/go/src go1.13 `encoding/xml`/.Unmarshalscip-go gomod github.com/golang/go/src go1.13 `encoding/xml`/Unmarshal().(b.Bytesscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#Bytes().(), &rawBody); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | break |
| 87 | } |
| 88 | |
| 89 | e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError. = &azurescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ServiceError#{ |
| 90 | Codescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ServiceError#Code.: "Unknown", |
| 91 | Messagescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ServiceError#Message.: "Unknown service error", |
| 92 | } |
| 93 | if len(rawBody) > 0 { |
| 94 | e.ServiceErrorscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#ServiceError..Detailsscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ServiceError#Details. = []map[string]interface{}{rawBody} |
| 95 | } |
| 96 | } |
| 97 | e.Responsescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/DetailedError#Response. = resp |
| 98 | e.RequestIDscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/RequestError#RequestID. = azurescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/.ExtractRequestIDscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest/azure`/ExtractRequestID().(resp) |
| 99 | if e.StatusCodescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/DetailedError#StatusCode. == nil { |
| 100 | e.StatusCodescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/DetailedError#StatusCode. = resp.StatusCodescip-go gomod github.com/golang/go/src go1.13 `net/http`/Response#StatusCode. |
| 101 | } |
| 102 | err = &e |
| 103 | } |
| 104 | return err |
| 105 | }) |
| 106 | } |
| 107 | } |