From 4f97cb9a42addb4d3d2ba325ca91f300871753ce Mon Sep 17 00:00:00 2001 From: mleku Date: Tue, 28 Oct 2025 20:26:09 +0000 Subject: [PATCH] Improve error handling and logging in message processing - Updated HandleMessage function to include the actual message content in error notices for invalid messages, enhancing clarity for debugging. - Added nil check in Value method of tag encoder to prevent potential nil pointer dereference. - Refactored tag handling in spider to utilize hex-encoded pubkeys for improved compatibility and clarity in filter creation. - bump to v0.19.10 --- app/handle-message.go | 4 +--- pkg/encoders/tag/tag.go | 1 + pkg/protocol/ws/client.go | 2 +- pkg/spider/spider.go | 27 +++++++++++++++------------ 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/app/handle-message.go b/app/handle-message.go index 6244b1f..5ae86cd 100644 --- a/app/handle-message.go +++ b/app/handle-message.go @@ -75,9 +75,7 @@ func (l *Listener) HandleMessage(msg []byte, remote string) { // Validate message for invalid characters before processing if err := validateJSONMessage(msg); err != nil { log.E.F("%s message validation FAILED (len=%d): %v", remote, len(msg), err) - // Don't log the actual message content as it contains binary data - // Send generic error notice to client - if noticeErr := noticeenvelope.NewFrom("invalid message format: contains invalid characters").Write(l); noticeErr != nil { + if noticeErr := noticeenvelope.NewFrom(fmt.Sprintf("invalid message format: contains invalid characters: %s", msg)).Write(l); noticeErr != nil { log.E.F("%s failed to send validation error notice: %v", remote, noticeErr) } return diff --git a/pkg/encoders/tag/tag.go b/pkg/encoders/tag/tag.go index e76e419..7c19020 100644 --- a/pkg/encoders/tag/tag.go +++ b/pkg/encoders/tag/tag.go @@ -144,6 +144,7 @@ func (t *T) Key() (key []byte) { } func (t *T) Value() (key []byte) { + if t==nil {return} if len(t.T) > Value { return t.T[Value] } diff --git a/pkg/protocol/ws/client.go b/pkg/protocol/ws/client.go index 5fd9706..91e8fb3 100644 --- a/pkg/protocol/ws/client.go +++ b/pkg/protocol/ws/client.go @@ -307,7 +307,7 @@ func (r *Client) ConnectWithTLS( if r.notices != nil { r.notices <- env.Message } else { - log.E.F("NOTICE from %s: '%s'\n", r.URL, env.Message) + log.E.F("NOTICE from %s: '%s'", r.URL, env.Message) } case authenvelope.L: env := authenvelope.NewChallenge() diff --git a/pkg/spider/spider.go b/pkg/spider/spider.go index 05cd7ff..7c0e3a6 100644 --- a/pkg/spider/spider.go +++ b/pkg/spider/spider.go @@ -2,7 +2,6 @@ package spider import ( "context" - "encoding/hex" "fmt" "sync" "time" @@ -12,6 +11,7 @@ import ( "lol.mleku.dev/log" "next.orly.dev/pkg/database" "next.orly.dev/pkg/encoders/filter" + "next.orly.dev/pkg/encoders/hex" "next.orly.dev/pkg/encoders/tag" "next.orly.dev/pkg/encoders/timestamp" "next.orly.dev/pkg/interfaces/publisher" @@ -414,17 +414,20 @@ func (rc *RelayConnection) createBatchSubscription(batchID string, pubkeys [][]b } // Create filters: one for authors, one for p tags - var pTags tag.S + // For #p tag filters, all pubkeys must be in a single tag array as hex-encoded strings + tagElements := [][]byte{[]byte("p")} // First element is the key for _, pk := range pubkeys { - pTags = append(pTags, tag.NewFromAny("p", pk)) + pkHex := hex.EncAppend(nil, pk) + tagElements = append(tagElements, pkHex) } + pTags := &tag.S{tag.NewFromBytesSlice(tagElements...)} filters := filter.NewS( &filter.F{ Authors: tag.NewFromBytesSlice(pubkeys...), }, &filter.F{ - Tags: tag.NewS(pTags...), + Tags: pTags, }, ) @@ -465,10 +468,6 @@ func (bs *BatchSubscription) handleEvents() { // Save event to database if _, err := bs.relay.spider.db.SaveEvent(bs.relay.ctx, ev); err != nil { - if !chk.E(err) { - log.T.F("spider: saved event %s from %s", - hex.EncodeToString(ev.ID[:]), bs.relay.url) - } } else { // Publish event if it was newly saved if bs.relay.spider.pub != nil { @@ -527,10 +526,14 @@ func (rc *RelayConnection) performCatchup(sub *BatchSubscription, disconnectTime sinceTs := timestamp.T{V: since.Unix()} untilTs := timestamp.T{V: until.Unix()} - var pTags tag.S + // Create filters with hex-encoded pubkeys for #p tags + // All pubkeys must be in a single tag array + tagElements := [][]byte{[]byte("p")} // First element is the key for _, pk := range sub.pubkeys { - pTags = append(pTags, tag.NewFromAny("p", pk)) + pkHex := hex.EncAppend(nil, pk) + tagElements = append(tagElements, pkHex) } + pTags := &tag.S{tag.NewFromBytesSlice(tagElements...)} filters := filter.NewS( &filter.F{ @@ -539,7 +542,7 @@ func (rc *RelayConnection) performCatchup(sub *BatchSubscription, disconnectTime Until: &untilTs, }, &filter.F{ - Tags: tag.NewS(pTags...), + Tags: pTags, Since: &sinceTs, Until: &untilTs, }, @@ -582,7 +585,7 @@ func (rc *RelayConnection) performCatchup(sub *BatchSubscription, disconnectTime if _, err := rc.spider.db.SaveEvent(rc.ctx, ev); err != nil { if !chk.E(err) { log.T.F("spider: catch-up saved event %s from %s", - hex.EncodeToString(ev.ID[:]), rc.url) + hex.Enc(ev.ID[:]), rc.url) } } else { // Publish event if it was newly saved