Skip to content

Commit

Permalink
pass errors all the way back
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Nov 2, 2018
1 parent 8a882cc commit 068fdb7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"errors"
"fmt"
"io"
"io/ioutil"
Expand All @@ -13,6 +12,7 @@ import (
"time"

humanize "github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/schollz/croc/src/croc"
"github.com/schollz/croc/src/utils"
"github.com/skratchdot/open-golang/open"
Expand Down Expand Up @@ -131,7 +131,7 @@ func Run() {

err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "\nerror: %s", err.Error())
fmt.Fprintf(os.Stderr, "\r\n%s", err.Error())
}
fmt.Fprintf(os.Stderr, "\r\n")
}
Expand Down
14 changes: 4 additions & 10 deletions src/croc/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ import (
var DebugLevel string

// Receive is the async operation to receive a file
func (cr *Croc) startRecipient(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan struct{}, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) {
func (cr *Croc) startRecipient(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan error, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) {
logger.SetLogLevel(DebugLevel)
err := cr.receive(forceSend, serverAddress, tcpPorts, isLocal, c, codephrase, noPrompt, useStdout)
if err != nil {
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
fmt.Fprintf(os.Stderr, "\n"+err.Error())
cr.StateString = err.Error()
err = errors.Wrap(err, "error in recipient:")
c.WriteMessage(websocket.TextMessage, []byte(err.Error()))
time.Sleep(50 * time.Millisecond)
}
if err != nil && strings.HasPrefix(err.Error(), "websocket: close 100") {
err = nil
}
done <- struct{}{}
done <- err
}

func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, codephrase string, noPrompt bool, useStdout bool) (err error) {
Expand Down
18 changes: 5 additions & 13 deletions src/croc/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,14 @@ import (
)

// Send is the async call to send data
func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan struct{}, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) {
func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, done chan error, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) {
logger.SetLogLevel(DebugLevel)
log.Debugf("sending %s", fname)
err := cr.send(forceSend, serverAddress, tcpPorts, isLocal, c, fname, codephrase, useCompression, useEncryption)
if err != nil {
log.Debug(err)
if !strings.HasPrefix(err.Error(), "websocket: close 100") {
fmt.Fprintf(os.Stderr, "\n"+err.Error())
err = errors.Wrap(err, "error in sender:")
c.WriteMessage(websocket.TextMessage, []byte(err.Error()))
time.Sleep(50 * time.Millisecond)
cr.StateString = err.Error()
}
if err != nil && strings.HasPrefix(err.Error(), "websocket: close 100") {
err = nil
}

done <- struct{}{}
done <- err
}

func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isLocal bool, c *websocket.Conn, fname string, codephrase string, useCompression bool, useEncryption bool) (err error) {
Expand Down Expand Up @@ -128,7 +120,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL
if messageType == websocket.PongMessage || messageType == websocket.PingMessage {
continue
}
if messageType == websocket.TextMessage && bytes.Equal(message, []byte("interrupt")) {
if messageType == websocket.TextMessage && bytes.HasPrefix(message, []byte("interrupt")) {
return errors.New("\rinterrupted by other party")
}
if messageType == websocket.TextMessage && bytes.HasPrefix(message, []byte("err")) {
Expand Down
11 changes: 8 additions & 3 deletions src/croc/sending.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (c *Croc) sendReceive(address, websocketPort string, tcpPorts []string, fna
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

done := make(chan struct{})
done := make(chan error)
// connect to server
websocketAddress := ""
if len(websocketPort) > 0 {
Expand Down Expand Up @@ -173,9 +173,14 @@ func (c *Croc) sendReceive(address, websocketPort string, tcpPorts []string, fna

for {
select {
case <-done:
case doneError := <-done:
log.Debug("received done signal")
return nil
if doneError != nil {
c.StateString = doneError.Error()
sock.WriteMessage(websocket.TextMessage, []byte("error: "+doneError.Error()))
time.Sleep(50 * time.Millisecond)
}
return doneError
case <-interrupt:
if !c.Debug {
SetDebugLevel("critical")
Expand Down

0 comments on commit 068fdb7

Please sign in to comment.