- Notifications
You must be signed in to change notification settings - Fork 3
Description
This package gives the ability to provide detailed error messages, but in practice this is somewhat awkward to use with the current API. Consider the case where a caller gets a non-OK status code back, but is unsure whether a detailed error is present. In this case, the caller must differentiate a failure of jsonresp.ReadResponse to unmarshal versus a detailed error itself. In the case of a failure to unmarshal, it is often desirable to report other more relevant information such as the HTTP status code. This can currently be accomplished with logic such as:
if res.StatusCode != http.StatusOK { if err := jsonresp.ReadResponse(res.Body, nil); err != nil { if err, ok := err.(*jsonresp.Error); ok { return err } } return jsonresp.NewError(res.StatusCode, "") } This is not very straight forward. To help with clarity, I propose introducing a func ReadError(r io.Reader) error function, that will return the error detail when one is possible in r, and nil otherwise. Then, the logic is more readable:
if res.StatusCode != http.StatusOK { if err := jsonresp.ReadError(res.Body); err != nil { return err } return jsonresp.NewError(res.StatusCode, "") }