(method, url string, data []byte)
| 128 | } |
| 129 | |
| 130 | func executeCommand(method, url string, data []byte) (json.RawMessage, error) { |
| 131 | debugLog("-> %s %s\n%s", method, filteredURL(url), data) |
| 132 | request, err := newRequest(method, url, data) |
| 133 | if err != nil { |
| 134 | return nil, err |
| 135 | } |
| 136 | |
| 137 | response, err := HTTPClient.Do(request) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | |
| 142 | buf, err := ioutil.ReadAll(response.Body) |
| 143 | if debugFlag { |
| 144 | if err == nil { |
| 145 | // Pretty print the JSON response |
| 146 | var prettyBuf bytes.Buffer |
| 147 | if err = json.Indent(&prettyBuf, buf, "", " "); err == nil && prettyBuf.Len() > 0 { |
| 148 | buf = prettyBuf.Bytes() |
| 149 | } |
| 150 | } |
| 151 | debugLog("<- %s [%s]\n%s", response.Status, response.Header["Content-Type"], buf) |
| 152 | } |
| 153 | if err != nil { |
| 154 | return nil, errors.New(response.Status) |
| 155 | } |
| 156 | |
| 157 | fullCType := response.Header.Get("Content-Type") |
| 158 | cType, _, err := mime.ParseMediaType(fullCType) |
| 159 | if err != nil { |
| 160 | return nil, fmt.Errorf("got content type header %q, expected %q", fullCType, jsonContentType) |
| 161 | } |
| 162 | if cType != jsonContentType { |
| 163 | return nil, fmt.Errorf("got content type %q, expected %q", cType, jsonContentType) |
| 164 | } |
| 165 | |
| 166 | reply := new(serverReply) |
| 167 | if err := json.Unmarshal(buf, reply); err != nil { |
| 168 | if response.StatusCode != http.StatusOK { |
| 169 | return nil, fmt.Errorf("bad server reply status: %s", response.Status) |
| 170 | } |
| 171 | return nil, err |
| 172 | } |
| 173 | if reply.Err != "" { |
| 174 | return nil, &reply.Error |
| 175 | } |
| 176 | |
| 177 | // Handle the W3C-compliant error format. In the W3C spec, the error is |
| 178 | // embedded in the 'value' field. |
| 179 | if len(reply.Value) > 0 { |
| 180 | respErr := new(Error) |
| 181 | if err := json.Unmarshal(reply.Value, respErr); err == nil && respErr.Err != "" { |
| 182 | respErr.HTTPCode = response.StatusCode |
| 183 | return nil, respErr |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Handle the legacy error format. |
no test coverage detected
searching dependent graphs…