chore: handle errors in wsproxy server for cli using buildinfo (#11584)

Cli errors are pretty formatted. This handles nested pretty types. Before it found the first error it could understand and return that. Now it will print the full error stack with more information.

To prevent information loss, a "[Trace=...]" was added to capture some extra error context for debugging.
This commit is contained in:
Steven Masley
2024-01-11 16:55:34 -06:00
committed by GitHub
parent aecdafdcf2
commit f5a9f5ca3d
5 changed files with 114 additions and 31 deletions
+13 -4
View File
@@ -323,6 +323,17 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
return resp, err
}
// ExpectJSONMime is a helper function that will assert the content type
// of the response is application/json.
func ExpectJSONMime(res *http.Response) error {
contentType := res.Header.Get("Content-Type")
mimeType := parseMimeType(contentType)
if mimeType != "application/json" {
return xerrors.Errorf("unexpected non-JSON response %q", contentType)
}
return nil
}
// ReadBodyAsError reads the response as a codersdk.Response, and
// wraps it in a codersdk.Error type for easy marshaling.
func ReadBodyAsError(res *http.Response) error {
@@ -330,7 +341,6 @@ func ReadBodyAsError(res *http.Response) error {
return xerrors.Errorf("no body returned")
}
defer res.Body.Close()
contentType := res.Header.Get("Content-Type")
var requestMethod, requestURL string
if res.Request != nil {
@@ -352,8 +362,7 @@ func ReadBodyAsError(res *http.Response) error {
return xerrors.Errorf("read body: %w", err)
}
mimeType := parseMimeType(contentType)
if mimeType != "application/json" {
if mimeErr := ExpectJSONMime(res); mimeErr != nil {
if len(resp) > 2048 {
resp = append(resp[:2048], []byte("...")...)
}
@@ -365,7 +374,7 @@ func ReadBodyAsError(res *http.Response) error {
method: requestMethod,
url: requestURL,
Response: Response{
Message: fmt.Sprintf("unexpected non-JSON response %q", contentType),
Message: mimeErr.Error(),
Detail: string(resp),
},
Helper: helpMessage,
+1 -1
View File
@@ -2065,7 +2065,7 @@ func (c *Client) BuildInfo(ctx context.Context) (BuildInfoResponse, error) {
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode != http.StatusOK || ExpectJSONMime(res) != nil {
return BuildInfoResponse{}, ReadBodyAsError(res)
}