be smarter about errors from SaveEvent()

This commit is contained in:
fiatjaf 2023-06-26 20:47:33 -03:00
parent 0a5624737d
commit 0d92cb63d1
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1

View File

@ -3,11 +3,14 @@ package relayer
import (
"context"
"fmt"
"regexp"
"github.com/fiatjaf/relayer/v2/storage"
"github.com/nbd-wtf/go-nostr"
)
var nip20prefixmatcher = regexp.MustCompile(`^\w+: `)
// AddEvent has a business rule to add an event to the relayer
func AddEvent(ctx context.Context, relay Relay, evt *nostr.Event) (accepted bool, message string) {
if evt == nil {
@ -33,7 +36,12 @@ func AddEvent(ctx context.Context, relay Relay, evt *nostr.Event) (accepted bool
case storage.ErrDupEvent:
return true, saveErr.Error()
default:
return false, fmt.Sprintf("error: failed to save: %s", saveErr.Error())
errmsg := saveErr.Error()
if nip20prefixmatcher.MatchString(errmsg) {
return false, errmsg
} else {
return false, fmt.Sprintf("error: failed to save (%s)", errmsg)
}
}
}