mirror of https://github.com/schollz/croc.git
pass errors all the way back
This commit is contained in:
parent
8a882cc174
commit
068fdb77fc
|
@ -1,7 +1,6 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
@ -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"
|
||||
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
@ -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")) {
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue