Fix bug when endpoint is invalid or not configured proeprly and fails lookup (#171)

Fixes #169

This now behaves like this:

```
/Users/prologic/Projects/saltyim/saltyim # ./salty-chat write prologic@mills.io test
WARN[0000] error looking up user endpoint                error="error looking up user testing123@shortcircuit.net.au: non-2xx response received: 404 Not Found"
error initializing client: unable to find your endpoint for testing123@shortcircuit.net.au
/Users/prologic/Projects/saltyim/saltyim #
```

Co-authored-by: James Mills <prologic@shortcircuit.net.au>
Reviewed-on: #171
Reviewed-by: m u t e f a l l <mutefall@noreply@mills.io>
pull/173/head
James Mills 5 months ago
parent dc432abdc3
commit 9c5fec5caf
  1. 16
      client.go
  2. 13
      client_test.go
  3. 3
      cmd/salty-chat/avatar.go
  4. 2
      cmd/salty-chat/chat.go
  5. 2
      cmd/salty-chat/makeuser.go
  6. 2
      cmd/salty-chat/read.go
  7. 3
      cmd/salty-chat/register.go
  8. 3
      cmd/salty-chat/send.go

@ -126,16 +126,20 @@ func NewClient(me *Addr, options ...ClientOption) (*Client, error) {
return nil, ErrMissingIdentity
}
if me == nil || me.IsZero() {
me = cli.id.addr
if cli.me == nil || cli.me.IsZero() {
cli.me = cli.id.addr
}
if me == nil || me.IsZero() {
return nil, fmt.Errorf("unable to find your user addressn in %s", cli.id.Source())
if err := cli.me.Refresh(); err != nil {
log.WithError(err).Warn("error looking up user endpoint")
}
if err := me.Refresh(); err != nil {
log.WithError(err).Warn("error looking up user endpoint")
if cli.me == nil || cli.me.IsZero() {
return nil, fmt.Errorf("unable to find your user address in %s", cli.id.Source())
}
if cli.me.Endpoint() == nil {
return nil, fmt.Errorf("unable to find your endpoint for %s", cli.me.String())
}
log.Debugf("Using identity %s with public key %s", cli.id.Source(), cli.id.key)

@ -7,9 +7,22 @@ import (
"testing"
"github.com/keys-pub/keys"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClient_InvalidEndpoint(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
me, err := ParseAddr("foo@example.com")
require.NoError(err)
assert.NotNil(me)
_, err = NewClient(me)
assert.Error(err)
}
func TestClient_Outbox(t *testing.T) {
test := require.New(t)

@ -4,6 +4,7 @@ import (
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/saltyim"
@ -31,7 +32,7 @@ NOTE: This is only spported on a Salty Broker.`,
me, err := saltyim.ParseAddr(user)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

@ -35,7 +35,7 @@ messages to the user via their discovered endpoint.`,
me, err := saltyim.ParseAddr(user)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

@ -70,7 +70,7 @@ NOTE: The <endpoint> argument will override and -u/--endpoint flag set.`,
me, err := saltyim.ParseAddr(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

@ -35,7 +35,7 @@ not specified defaults to the local user ($USER)`,
me, err := saltyim.ParseAddr(user)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

@ -5,6 +5,7 @@ import (
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.mills.io/saltyim"
@ -27,7 +28,7 @@ NOTE: This is only spported o a Salty Broker.`,
me, err := saltyim.ParseAddr(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

@ -7,6 +7,7 @@ import (
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -48,7 +49,7 @@ https://mills.io/.well-known/salty/prologic.json`,
me, err := saltyim.ParseAddr(user)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing addr: %s\n", err)
log.Debugf("error parsing addr: %s\n", err)
}
// XXX: What if me.IsZero()

Loading…
Cancel
Save