fix error handling in dial function

This commit is contained in:
dd dd 2024-11-08 14:54:08 +01:00
parent 646495de4c
commit ce9a3758a4

View File

@ -3,12 +3,13 @@ package netstr
import ( import (
"context" "context"
"fmt" "fmt"
"log/slog"
"net"
"github.com/asmogo/nws/config" "github.com/asmogo/nws/config"
"github.com/asmogo/nws/protocol" "github.com/asmogo/nws/protocol"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nbd-wtf/go-nostr" "github.com/nbd-wtf/go-nostr"
"log/slog"
"net"
) )
type DialOptions struct { type DialOptions struct {
@ -25,7 +26,10 @@ type DialOptions struct {
// It creates a signed event using the private key, public key, and destination address. // It creates a signed event using the private key, public key, and destination address.
// It ensures that the relays are available in the pool and publishes the signed event to each relay. // It ensures that the relays are available in the pool and publishes the signed event to each relay.
// Finally, it returns the Connection and nil error. If there are any errors, nil connection and the error are returned. // Finally, it returns the Connection and nil error. If there are any errors, nil connection and the error are returned.
func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context.Context, net_, addr string) (net.Conn, error) { func DialSocks(
options DialOptions,
config *config.EntryConfig,
) func(ctx context.Context, net_, addr string) (net.Conn, error) {
return func(ctx context.Context, net_, addr string) (net.Conn, error) { return func(ctx context.Context, net_, addr string) (net.Conn, error) {
key := nostr.GeneratePrivateKey() key := nostr.GeneratePrivateKey()
connection := NewConnection(ctx, connection := NewConnection(ctx,
@ -51,7 +55,7 @@ func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context
// create nostr signed event // create nostr signed event
signer, err := protocol.NewEventSigner(key) signer, err := protocol.NewEventSigner(key)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error creating signer: %w", err)
} }
opts := []protocol.MessageOption{ opts := []protocol.MessageOption{
protocol.WithType(options.MessageType), protocol.WithType(options.MessageType),
@ -67,14 +71,15 @@ func DialSocks(options DialOptions, config *config.EntryConfig) func(ctx context
opts...) opts...)
for _, relayUrl := range relays { for _, relayUrl := range relays {
relay, err := options.Pool.EnsureRelay(relayUrl) var relay *nostr.Relay
relay, err = options.Pool.EnsureRelay(relayUrl)
if err != nil { if err != nil {
slog.Error("error creating relay", err) slog.Error("error creating relay", err)
continue continue
} }
err = relay.Publish(ctx, ev) err = relay.Publish(ctx, ev)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("error publishing event: %w", err)
} }
} }
return connection, nil return connection, nil