MCPcopy
hub / github.com/benbjohnson/wtf / parseResponseError

Function parseResponseError

http/http.go:115–136  ·  view source on GitHub ↗

parseResponseError parses an JSON-formatted error response.

(resp *http.Response)

Source from the content-addressed store, hash-verified

113
114// parseResponseError parses an JSON-formatted error response.
115func parseResponseError(resp *http.Response) error {
116 defer resp.Body.Close()
117
118 // Read the response body so we can reuse it for the error message if it
119 // fails to decode as JSON.
120 buf, err := ioutil.ReadAll(resp.Body)
121 if err != nil {
122 return err
123 }
124
125 // Parse JSON formatted error response.
126 // If not JSON, use the response body as the error message.
127 var errorResponse ErrorResponse
128 if err := json.Unmarshal(buf, &errorResponse); err != nil {
129 message := strings.TrimSpace(string(buf))
130 if message == "" {
131 message = "Empty response from server."
132 }
133 return wtf.Errorf(FromErrorStatusCode(resp.StatusCode), message)
134 }
135 return wtf.Errorf(FromErrorStatusCode(resp.StatusCode), errorResponse.Error)
136}
137
138// LogError logs an error with the HTTP route information.
139func LogError(r *http.Request, err error) {

Callers 5

FindDialByIDMethod · 0.85
FindDialsMethod · 0.85
CreateDialMethod · 0.85
DeleteDialMethod · 0.85

Calls 3

ErrorfFunction · 0.92
FromErrorStatusCodeFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected