Debian Codegraph @

storage/internal/helpers/errors.go

raw symbols SCIP
1 package helpers
2
3 import (
4 "encoding/json"
5 "encoding/xml"
6 "fmt"
7 "io/ioutil"
8 "net/http"
9 "strings"
10
11 "github.com/Azure/go-autorest/autorest"
12 "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 WithErrorUnlessStatusCode(codes ...int) autorest.RespondDecorator {
29 return func(r autorest.Responder) autorest.Responder {
30 return autorest.ResponderFunc(func(resp *http.Response) error {
31 err := r.Respond(resp)
32 if err == nil && !autorest.ResponseHasStatusCode(resp, codes...) {
33 var e azure.RequestError
34 defer resp.Body.Close()
35
36 contentType := autorest.EncodedAsJSON
37 if resp != nil {
38 contentTypeStr := resp.Header.Get("Content-Type")
39 if strings.EqualFold(contentTypeStr, "application/xml") {
40 contentType = 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 := autorest.CopyAndDecode(contentType, resp.Body, &e)
47 resp.Body = ioutil.NopCloser(&b)
48 if decodeErr != nil {
49 return fmt.Errorf("autorest/azure: error response cannot be parsed: %q error: %v", b.String(), decodeErr)
50 }
51 if e.ServiceError == nil {
52 switch contentType {
53 case autorest.EncodedAsJSON:
54 // Check if error is unwrapped ServiceError
55 if err := json.Unmarshal(b.Bytes(), &e.ServiceError); err != nil {
56 return err
57 }
58 break
59 case autorest.EncodedAsXML:
60 // Check if error is unwrapped ServiceError
61 if err := xml.Unmarshal(b.Bytes(), &e.ServiceError); err != nil {
62 return err
63 }
64 break
65 }
66 }
67 if e.ServiceError.Message == "" {
68
69 rawBody := map[string]interface{}{}
70
71 switch contentType {
72 case 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 := json.Unmarshal(b.Bytes(), &rawBody); err != nil {
76 return err
77 }
78 break
79
80 case 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 := xml.Unmarshal(b.Bytes(), &rawBody); err != nil {
84 return err
85 }
86 break
87 }
88
89 e.ServiceError = &azure.ServiceError{
90 Code: "Unknown",
91 Message: "Unknown service error",
92 }
93 if len(rawBody) > 0 {
94 e.ServiceError.Details = []map[string]interface{}{rawBody}
95 }
96 }
97 e.Response = resp
98 e.RequestID = azure.ExtractRequestID(resp)
99 if e.StatusCode == nil {
100 e.StatusCode = resp.StatusCode
101 }
102 err = &e
103 }
104 return err
105 })
106 }
107 }