Skip to content

Commit

Permalink
DXCDT-550: Propagate errors through websocket connection (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiught committed Nov 1, 2023
1 parent d9b36fe commit 8bea225
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions internal/cli/universal_login_customize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
loadBrandingMessageType = "LOAD_BRANDING"
fetchPromptMessageType = "FETCH_PROMPT"
saveBrandingMessageType = "SAVE_BRANDING"
errorMessageType = "ERROR"
)

type (
Expand Down Expand Up @@ -342,16 +343,21 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

connection, err := upgrader.Upgrade(w, r, nil)
if err != nil {
h.display.Errorf("failed to upgrade the connection to the WebSocket protocol: %v", err)
h.display.Errorf("Failed to upgrade the connection to the WebSocket protocol: %v", err)
h.display.Warnf("Try restarting the command.")
h.shutdown()
return
}
defer func() {
_ = connection.Close()
}()

connection.SetReadLimit(1e+6) // 1 MB.

payload, err := json.Marshal(&h.brandingData)
if err != nil {
h.display.Errorf("failed to encode the branding data to json: %v", err)
h.display.Errorf("Failed to encode the branding data to json: %v", err)
h.display.Warnf("Try restarting the command.")
h.shutdown()
return
}
Expand All @@ -362,23 +368,30 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if err = connection.WriteJSON(loadBrandingMsg); err != nil {
h.display.Errorf("failed to send branding data message: %v", err)
h.display.Errorf("Failed to send branding data message: %v", err)
h.display.Warnf("Try restarting the command.")
h.shutdown()
return
}

for {
var message webSocketMessage
if err := connection.ReadJSON(&message); err != nil {
h.display.Errorf("failed to read WebSocket message: %v", err)
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) ||
websocket.IsUnexpectedCloseError(err, websocket.CloseAbnormalClosure) {
// The connection was closed.
break
}

h.display.Errorf("Failed to read WebSocket message: %v", err)
continue
}

switch message.Type {
case fetchPromptMessageType:
var promptToFetch promptData
if err := json.Unmarshal(message.Payload, &promptToFetch); err != nil {
h.display.Errorf("failed to unmarshal fetch prompt payload: %v", err)
h.display.Errorf("Failed to unmarshal fetch prompt payload: %v", err)
continue
}

Expand All @@ -389,7 +402,17 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
promptToFetch.Language,
)
if err != nil {
h.display.Errorf("failed to fetch custom text for prompt: %v", err)
h.display.Errorf("Failed to fetch custom text for prompt: %v", err)

errorMsg := webSocketMessage{
Type: errorMessageType,
Payload: []byte(fmt.Sprintf(`{"error":%q}`, err.Error())),
}

if err := connection.WriteJSON(errorMsg); err != nil {
h.display.Errorf("Failed to send error message: %v", err)
}

continue
}

Expand All @@ -406,18 +429,28 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if err = connection.WriteJSON(fetchPromptMsg); err != nil {
h.display.Errorf("failed to send prompt data message: %v", err)
h.display.Errorf("Failed to send prompt data message: %v", err)
continue
}
case saveBrandingMessageType:
var saveBrandingMsg universalLoginBrandingData
if err := json.Unmarshal(message.Payload, &saveBrandingMsg); err != nil {
h.display.Errorf("failed to unmarshal save branding data payload: %v", err)
h.display.Errorf("Failed to unmarshal save branding data payload: %v", err)
continue
}

if err := saveUniversalLoginBrandingData(r.Context(), h.api, &saveBrandingMsg); err != nil {
h.display.Errorf("failed to save branding data: %v", err)
h.display.Errorf("Failed to save branding data: %v", err)

errorMsg := webSocketMessage{
Type: errorMessageType,
Payload: []byte(fmt.Sprintf(`{"error":%q}`, err.Error())),
}

if err := connection.WriteJSON(errorMsg); err != nil {
h.display.Errorf("Failed to send error message: %v", err)
}

continue
}
}
Expand Down

0 comments on commit 8bea225

Please sign in to comment.