Skip to content

Commit

Permalink
Merge pull request #90 from getAlby/task-error-logs
Browse files Browse the repository at this point in the history
chore: add error logs
  • Loading branch information
im-adithya authored Jul 15, 2024
2 parents 4722bbe + f4ddb32 commit 8ee471d
Showing 1 changed file with 56 additions and 17 deletions.
73 changes: 56 additions & 17 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func (svc *Service) InfoHandler(c echo.Context) error {

relay, isCustomRelay, err := svc.getRelayConnection(ctx, requestData.RelayUrl)
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"relay_url": requestData.RelayUrl,
"wallet_pubkey": requestData.WalletPubkey,
}).Error("Error connecting to relay")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Error connecting to relay",
Error: err.Error(),
Expand All @@ -186,6 +190,10 @@ func (svc *Service) InfoHandler(c echo.Context) error {
}
sub, err := relay.Subscribe(ctx, []nostr.Filter{filter})
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"relay_url": requestData.RelayUrl,
"wallet_pubkey": requestData.WalletPubkey,
}).Error("Error subscribing to relay")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Error subscribing to relay",
Error: err.Error(),
Expand Down Expand Up @@ -236,6 +244,10 @@ func (svc *Service) PublishHandler(c echo.Context) error {

relay, isCustomRelay, err := svc.getRelayConnection(ctx, requestData.RelayUrl)
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
}).Error("Error subscribing to relay")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Error connecting to relay",
Error: err.Error(),
Expand All @@ -254,8 +266,8 @@ func (svc *Service) PublishHandler(c echo.Context) error {
err = relay.Publish(ctx, *requestData.SignedEvent)
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
"event_id": requestData.SignedEvent.ID,
"relay_url": requestData.RelayUrl,
}).Error("Failed to publish event")

return c.JSON(http.StatusInternalServerError, ErrorResponse{
Expand Down Expand Up @@ -306,6 +318,11 @@ func (svc *Service) NIP47Handler(c echo.Context) error {
}).Info("Processing request event")

if svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).Find(&RequestEvent{}).RowsAffected != 0 {
svc.Logger.WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
}).Error("Event already processed")
return c.JSON(http.StatusBadRequest, NIP47Response{
State: EVENT_ALREADY_PROCESSED,
})
Expand All @@ -317,6 +334,11 @@ func (svc *Service) NIP47Handler(c echo.Context) error {
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
}).Error("Failed to store request event")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
Expand Down Expand Up @@ -395,13 +417,19 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
}

svc.Logger.WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
}).Info("Processing request event")

if svc.db.Where("nostr_id = ?", requestData.SignedEvent.ID).First(&RequestEvent{}).RowsAffected != 0 {
svc.Logger.WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
}).Error("Event already processed")
return c.JSON(http.StatusBadRequest, NIP47Response{
State: EVENT_ALREADY_PROCESSED,
})
Expand All @@ -413,6 +441,12 @@ func (svc *Service) NIP47WebhookHandler(c echo.Context) error {
}

if err := svc.db.Create(&requestEvent).Error; err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"request_event_id": requestData.SignedEvent.ID,
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
}).Error("Failed to store request event")
return c.JSON(http.StatusInternalServerError, ErrorResponse{
Message: "Failed to store request event",
Error: err.Error(),
Expand Down Expand Up @@ -501,6 +535,11 @@ func (svc *Service) NIP47NotificationHandler(c echo.Context) error {
err := svc.db.Create(&subscription).Error

if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"wallet_pubkey": requestData.WalletPubkey,
"relay_url": requestData.RelayUrl,
"webhook_url": requestData.WebhookUrl,
}).Error("Failed to store subscription")
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Failed to store subscription",
Error: err.Error(),
Expand Down Expand Up @@ -740,26 +779,26 @@ func (svc *Service) publishEvent(ctx context.Context, subscription *Subscription
sub.Unsub()
} else {
svc.Logger.WithFields(logrus.Fields{
"publish_status": REQUEST_EVENT_PUBLISH_CONFIRMED,
"event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"publish_status": REQUEST_EVENT_PUBLISH_CONFIRMED,
"event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Published request event successfully")
subscription.RequestEventDB.State = REQUEST_EVENT_PUBLISH_CONFIRMED
}
}

func (svc *Service) handleResponseEvent(event *nostr.Event, subscription *Subscription) {
svc.Logger.WithFields(logrus.Fields{
"event_id": event.ID,
"event_kind": event.Kind,
"request_event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
"event_id": event.ID,
"event_kind": event.Kind,
"request_event_id": subscription.RequestEvent.ID,
"wallet_pubkey": svc.getWalletPubkey(subscription.Authors),
}).Info("Received response event")
responseEvent := ResponseEvent{
NostrId: event.ID,
Content: event.Content,
RepliedAt: event.CreatedAt.Time(),
RequestId: &subscription.RequestEventDB.ID,
NostrId: event.ID,
Content: event.Content,
RepliedAt: event.CreatedAt.Time(),
RequestId: &subscription.RequestEventDB.ID,
}
svc.db.Save(&responseEvent)
if subscription.WebhookUrl != "" {
Expand Down

0 comments on commit 8ee471d

Please sign in to comment.