chore: improve error messages in WebsocketApiClient.SendMessage

This commit is contained in:
nuk-eng-prod-service 2024-03-18 13:25:16 +01:00
parent 7470368137
commit fed62e88cf
2 changed files with 16 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/avast/retry-go/v4"
"github.com/google/uuid"
"github.com/twofas/2fas-server/internal/api/browser_extension/domain"
"github.com/twofas/2fas-server/internal/api/mobile/adapters"
"github.com/twofas/2fas-server/internal/common/logging"
@ -47,11 +48,12 @@ func (h *Send2FaTokenHandler) Handle(ctx context.Context, cmd *Send2FaToken) err
extId, _ := uuid.Parse(cmd.ExtensionId)
log := logging.FromContext(ctx)
log.WithFields(logging.Fields{
log = log.WithFields(logging.Fields{
"browser_extension_id": cmd.ExtensionId,
"device_id": cmd.DeviceId,
"token_request_id": cmd.TokenRequestId,
}).Info("Start command `Send2FaToken`")
})
log.Info("Start command `Send2FaToken`")
browserExtension, err := h.BrowserExtensionsRepository.FindById(extId)
@ -73,8 +75,8 @@ func (h *Send2FaTokenHandler) Handle(ctx context.Context, cmd *Send2FaToken) err
if err != nil {
log.WithFields(logging.Fields{
"error": err.Error(),
"message": message,
"error": err.Error(),
"websocket_message": message,
}).Error("Cannot send websocket message")
}

View File

@ -2,11 +2,13 @@ package websocket
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"path"
"github.com/gorilla/websocket"
app_http "github.com/twofas/2fas-server/internal/common/http"
"github.com/twofas/2fas-server/internal/common/logging"
)
@ -22,13 +24,15 @@ func NewWebsocketApiClient(websocketApiUrl string) *WebsocketApiClient {
}
func (ws *WebsocketApiClient) SendMessage(uri string, message interface{}) error {
u, _ := url.Parse(ws.wsAddr)
u, err := url.Parse(ws.wsAddr)
if err != nil {
return fmt.Errorf("failed to parse %q: %w", ws.wsAddr, err)
}
u.Path = path.Join(u.Path, uri)
msg, err := json.Marshal(message)
if err != nil {
return err
return fmt.Errorf("failed to marshal message: %w", err)
}
logging.WithFields(logging.Fields{
@ -41,23 +45,20 @@ func (ws *WebsocketApiClient) SendMessage(uri string, message interface{}) error
}
c, _, err := websocket.DefaultDialer.Dial(u.String(), requestHeaders)
if err != nil {
return err
return fmt.Errorf("failed to dial: %q: %w", u.String(), err)
}
err = c.WriteMessage(websocket.TextMessage, msg)
if err != nil {
logging.WithField("error", err.Error()).Error("Cannot send websocket message")
return err
return fmt.Errorf("failed to write message to the conection: %w", err)
}
err = c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
logging.WithField("error", err.Error()).Error("Cannot close websocket connection")
return err
return fmt.Errorf("failed to write close message to the conection: %w", err)
}
return nil