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
This commit is contained in:
@@ -75,9 +75,7 @@ func (l *Listener) HandleMessage(msg []byte, remote string) {
|
|||||||
// Validate message for invalid characters before processing
|
// Validate message for invalid characters before processing
|
||||||
if err := validateJSONMessage(msg); err != nil {
|
if err := validateJSONMessage(msg); err != nil {
|
||||||
log.E.F("%s message validation FAILED (len=%d): %v", remote, len(msg), err)
|
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
|
if noticeErr := noticeenvelope.NewFrom(fmt.Sprintf("invalid message format: contains invalid characters: %s", msg)).Write(l); noticeErr != nil {
|
||||||
// Send generic error notice to client
|
|
||||||
if noticeErr := noticeenvelope.NewFrom("invalid message format: contains invalid characters").Write(l); noticeErr != nil {
|
|
||||||
log.E.F("%s failed to send validation error notice: %v", remote, noticeErr)
|
log.E.F("%s failed to send validation error notice: %v", remote, noticeErr)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ func (t *T) Key() (key []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *T) Value() (key []byte) {
|
func (t *T) Value() (key []byte) {
|
||||||
|
if t==nil {return}
|
||||||
if len(t.T) > Value {
|
if len(t.T) > Value {
|
||||||
return t.T[Value]
|
return t.T[Value]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ func (r *Client) ConnectWithTLS(
|
|||||||
if r.notices != nil {
|
if r.notices != nil {
|
||||||
r.notices <- env.Message
|
r.notices <- env.Message
|
||||||
} else {
|
} 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:
|
case authenvelope.L:
|
||||||
env := authenvelope.NewChallenge()
|
env := authenvelope.NewChallenge()
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package spider
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -12,6 +11,7 @@ import (
|
|||||||
"lol.mleku.dev/log"
|
"lol.mleku.dev/log"
|
||||||
"next.orly.dev/pkg/database"
|
"next.orly.dev/pkg/database"
|
||||||
"next.orly.dev/pkg/encoders/filter"
|
"next.orly.dev/pkg/encoders/filter"
|
||||||
|
"next.orly.dev/pkg/encoders/hex"
|
||||||
"next.orly.dev/pkg/encoders/tag"
|
"next.orly.dev/pkg/encoders/tag"
|
||||||
"next.orly.dev/pkg/encoders/timestamp"
|
"next.orly.dev/pkg/encoders/timestamp"
|
||||||
"next.orly.dev/pkg/interfaces/publisher"
|
"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
|
// 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 {
|
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(
|
filters := filter.NewS(
|
||||||
&filter.F{
|
&filter.F{
|
||||||
Authors: tag.NewFromBytesSlice(pubkeys...),
|
Authors: tag.NewFromBytesSlice(pubkeys...),
|
||||||
},
|
},
|
||||||
&filter.F{
|
&filter.F{
|
||||||
Tags: tag.NewS(pTags...),
|
Tags: pTags,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -465,10 +468,6 @@ func (bs *BatchSubscription) handleEvents() {
|
|||||||
|
|
||||||
// Save event to database
|
// Save event to database
|
||||||
if _, err := bs.relay.spider.db.SaveEvent(bs.relay.ctx, ev); err != nil {
|
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 {
|
} else {
|
||||||
// Publish event if it was newly saved
|
// Publish event if it was newly saved
|
||||||
if bs.relay.spider.pub != nil {
|
if bs.relay.spider.pub != nil {
|
||||||
@@ -527,10 +526,14 @@ func (rc *RelayConnection) performCatchup(sub *BatchSubscription, disconnectTime
|
|||||||
sinceTs := timestamp.T{V: since.Unix()}
|
sinceTs := timestamp.T{V: since.Unix()}
|
||||||
untilTs := timestamp.T{V: until.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 {
|
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(
|
filters := filter.NewS(
|
||||||
&filter.F{
|
&filter.F{
|
||||||
@@ -539,7 +542,7 @@ func (rc *RelayConnection) performCatchup(sub *BatchSubscription, disconnectTime
|
|||||||
Until: &untilTs,
|
Until: &untilTs,
|
||||||
},
|
},
|
||||||
&filter.F{
|
&filter.F{
|
||||||
Tags: tag.NewS(pTags...),
|
Tags: pTags,
|
||||||
Since: &sinceTs,
|
Since: &sinceTs,
|
||||||
Until: &untilTs,
|
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 _, err := rc.spider.db.SaveEvent(rc.ctx, ev); err != nil {
|
||||||
if !chk.E(err) {
|
if !chk.E(err) {
|
||||||
log.T.F("spider: catch-up saved event %s from %s",
|
log.T.F("spider: catch-up saved event %s from %s",
|
||||||
hex.EncodeToString(ev.ID[:]), rc.url)
|
hex.Enc(ev.ID[:]), rc.url)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Publish event if it was newly saved
|
// Publish event if it was newly saved
|
||||||
|
|||||||
Reference in New Issue
Block a user