Skip to content

chore: add debug information to wsproxy errors #9683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use codersdk.ReadBodyAsError
  • Loading branch information
Emyrk committed Sep 20, 2023
commit bad257b8eb0741208bdc0ccb926f04fa0d726b47
34 changes: 14 additions & 20 deletions enterprise/coderd/proxyhealth/proxyhealth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -291,27 +290,22 @@ func (p *ProxyHealth) runOnce(ctx context.Context, now time.Time) (map[uuid.UUID
// Unhealthy as we did reach the proxy but it got an unexpected response.
status.Status = Unhealthy
var builder strings.Builder
// This string is shown on the UI where newlines are respected.
// This error message is not ever decoded programmatically, so keep it human-
// readable.
builder.WriteString(fmt.Sprintf("unexpected status code %d. ", resp.StatusCode))
contentType := resp.Header.Get("content-type")
body, _ := io.ReadAll(resp.Body)

switch {
case strings.Contains(contentType, "html"):
// Showing html payloads is useful, but we display this error message
// on our FE. An html payload is quite large and would be hard to
// format decently. Just tell the user to make the request themselves
// to observe the response.
builder.WriteString(fmt.Sprintf("\nThe response was html, which is unexpected. "+
"Send a request to %s from the Coderd environment to debug this issue.", reqURL))
case strings.Contains(contentType, "json"):
builder.WriteString(fmt.Sprintf("\nJSON response payload: %s", string(body)))
default:
// Just set an arbitrary cutoff point. The user can always debug with a manual request.
if len(body) < 1000 {
builder.WriteString(fmt.Sprintf("\n%q response payload: %s", contentType, string(body)))
builder.WriteString(fmt.Sprintf("\nEncountered error, send a request to %q from the Coderd environment to debug this issue.", reqURL))
err := codersdk.ReadBodyAsError(resp)
if err != nil {
var apiErr *codersdk.Error
if xerrors.As(err, &apiErr) {
builder.WriteString(fmt.Sprintf("\nError Message: %s\nError Detail: %s", apiErr.Message, apiErr.Detail))
for _, v := range apiErr.Validations {
// Pretty sure this is not possible from the called endpoint, but just in case.
builder.WriteString(fmt.Sprintf("\n\tValidation: %s=%s", v.Field, v.Detail))
}
} else {
builder.WriteString(fmt.Sprintf("\nThe response content type was %q, which is unexpected. "+
"Send a request to %s from the Coderd environment to debug this issue.", contentType, reqURL))
builder.WriteString(fmt.Sprintf("\nError: %s", err.Error()))
}
}

Expand Down