diff --git a/internal/api/mobile/app/command/send_2fa_token.go b/internal/api/mobile/app/command/send_2fa_token.go index fc50f8b..d87346d 100644 --- a/internal/api/mobile/app/command/send_2fa_token.go +++ b/internal/api/mobile/app/command/send_2fa_token.go @@ -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" @@ -45,13 +46,12 @@ type Send2FaTokenHandler struct { func (h *Send2FaTokenHandler) Handle(ctx context.Context, cmd *Send2FaToken) error { extId, _ := uuid.Parse(cmd.ExtensionId) - log := logging.FromContext(ctx) - - log.WithFields(logging.Fields{ + log := logging.FromContext(ctx).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 +73,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") } diff --git a/internal/common/websocket/gorilla_websocket_client.go b/internal/common/websocket/gorilla_websocket_client.go index 3e6fd27..e7903b5 100644 --- a/internal/common/websocket/gorilla_websocket_client.go +++ b/internal/common/websocket/gorilla_websocket_client.go @@ -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