chore: improve error messages in WebsocketApiClient.SendMessage (#37)

* chore: improve error messages in WebsocketApiClient.SendMessage

* fix logger variable

---------

Co-authored-by: Tobiasz Heller <tobiaszheller@gmail.com>
This commit is contained in:
Krzysztof Dryś 2024-03-26 20:40:13 +01:00 committed by GitHub
parent 7470368137
commit 6591f5c765
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 15 deletions

View File

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

View File

@ -2,11 +2,13 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
app_http "github.com/twofas/2fas-server/internal/common/http" app_http "github.com/twofas/2fas-server/internal/common/http"
"github.com/twofas/2fas-server/internal/common/logging" "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 { 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) u.Path = path.Join(u.Path, uri)
msg, err := json.Marshal(message) msg, err := json.Marshal(message)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to marshal message: %w", err)
} }
logging.WithFields(logging.Fields{ 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) c, _, err := websocket.DefaultDialer.Dial(u.String(), requestHeaders)
if err != nil { if err != nil {
return err return fmt.Errorf("failed to dial: %q: %w", u.String(), err)
} }
err = c.WriteMessage(websocket.TextMessage, msg) err = c.WriteMessage(websocket.TextMessage, msg)
if err != nil { if err != nil {
logging.WithField("error", err.Error()).Error("Cannot send websocket message") 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, "")) err = c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil { if err != nil {
logging.WithField("error", err.Error()).Error("Cannot close websocket connection") 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 return nil