mirror of
https://github.com/twofas/2fas-server.git
synced 2024-11-21 17:59:55 +01:00
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:
parent
7470368137
commit
6591f5c765
@ -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")
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user