Skip to content

Commit c61904a

Browse files
author
Matthew Fisher
committed
fix(errors): ensure unknown errors look nice
One of the complaints has been that the SDK now shows ugly unknown errors like this: > Creating build... Error: Unknown Error (400): map[detail:Post http://localhost:5555/v2/test-61453435/blobs/uploads/: net/http: transport closed before response was received] With this change, the error will dump the raw JSON output instead. > Creating build... Error: Unknown Error (400): {"detail": "Post http://localhost:5555/v2/test-61453435/blobs/uploads/: net/http: transport closed before response was received"}
1 parent 0bf2d46 commit c61904a

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

errors.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,16 @@ func checkForErrors(res *http.Response) error {
9292
res.Body.Close()
9393
}()
9494

95+
out, err := ioutil.ReadAll(res.Body)
96+
if err != nil {
97+
return unknownServerError(res.StatusCode, err.Error())
98+
}
99+
95100
switch res.StatusCode {
96101
case 400:
97102
bodyMap := make(map[string]interface{})
98-
if err := json.NewDecoder(res.Body).Decode(&bodyMap); err != nil {
99-
return unknownError(res.StatusCode, err)
103+
if err := json.Unmarshal(out, &bodyMap); err != nil {
104+
return unknownServerError(res.StatusCode, fmt.Sprintf("error decoding json response (%s): %s", err, string(out)))
100105
}
101106

102107
if scanResponse(bodyMap, "username", []string{fieldReqMsg, invalidUserMsg}, true) {
@@ -166,8 +171,7 @@ func checkForErrors(res *http.Response) error {
166171
return ErrTagNotFound
167172
}
168173
}
169-
170-
return unknownError(res.StatusCode, bodyMap)
174+
return unknownServerError(res.StatusCode, string(out))
171175
case 401:
172176
return ErrUnauthorized
173177
case 403:
@@ -183,11 +187,7 @@ func checkForErrors(res *http.Response) error {
183187
case 500:
184188
return ErrServerError
185189
default:
186-
out, err := ioutil.ReadAll(res.Body)
187-
if err != nil {
188-
return unknownError(res.StatusCode, err)
189-
}
190-
return unknownError(res.StatusCode, out)
190+
return unknownServerError(res.StatusCode, string(out))
191191
}
192192
}
193193

@@ -224,8 +224,8 @@ func arrayContains(search string, completeMatch bool, array []string) bool {
224224
return false
225225
}
226226

227-
func unknownError(sc int, k interface{}) error {
228-
return fmt.Errorf(formatErrUnknown, sc, k)
227+
func unknownServerError(statusCode int, message string) error {
228+
return fmt.Errorf(formatErrUnknown, statusCode, message)
229229
}
230230

231231
func scanResponse(

errors_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package deis
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
67
"net/http"
78
"testing"
@@ -257,12 +258,21 @@ func TestErrors(t *testing.T) {
257258
},
258259
expected: ErrServerError,
259260
},
261+
// ensure unknown errors at least look pretty
262+
errorTest{
263+
res: &http.Response{
264+
StatusCode: 400,
265+
Body: readCloser(`{"detail":"unknown error"}`),
266+
},
267+
expected: errors.New(`Unknown Error (400): {"detail":"unknown error"}`),
268+
},
260269
}
261270

262271
for _, check := range tests {
263272
actual := checkForErrors(check.res)
264273

265-
if actual != check.expected {
274+
// specifically check error output rather than value comparison
275+
if fmt.Sprintf("%v", actual) != fmt.Sprintf("%v", check.expected) {
266276
t.Errorf(failureMessage, check.expected, actual)
267277
}
268278
}

0 commit comments

Comments
 (0)