send / refactor(sftp): print errors from server to client #20

open · opened on 2024-08-20T03:19:05Z by erock
Help
# add changes to patch request
git format-patch main --stdout | ssh pr.pico.sh pr add 20
# add review to patch request
git format-patch main --stdout | ssh pr.pico.sh pr add --review 20
# remove patchset
ssh pr.pico.sh ps rm ps-x
# checkout all patches
ssh pr.pico.sh pr print 20 | git am -3
# print a diff between the last two patches in a patch request
ssh pr.pico.sh pr diff 20
# accept PR
ssh pr.pico.sh pr accept 20
# close PR
ssh pr.pico.sh pr close 20

Logs

erock created pr with ps-36 on 2024-08-20T03:19:05Z

Patchsets

Diff ↕
Most clients do not print the error from request server so we need to do
it ourselves.

reference: https://github.com/pkg/sftp/issues/219#issuecomment-358136231
 send/sftp/handler.go | 34 ++++++++++++++++++++++++++++++++++
 send/sftp/sftp.go    | 14 +++++++++-----
 2 files changed, 43 insertions(+), 5 deletions(-)
  1From 5d3b1a4494cc0cf8919894785171a3c2cc60cd57 Mon Sep 17 00:00:00 2001
  2From: Eric Bower <me@erock.io>
  3Date: Mon, 19 Aug 2024 23:16:02 -0400
  4Subject: [PATCH] refactor(sftp): print errors from server to client
  5
  6Most clients do not print the error from request server so we need to do
  7it ourselves.
  8
  9reference: https://github.com/pkg/sftp/issues/219#issuecomment-358136231
 10---
 11 send/sftp/handler.go | 34 ++++++++++++++++++++++++++++++++++
 12 send/sftp/sftp.go    | 14 +++++++++-----
 13 2 files changed, 43 insertions(+), 5 deletions(-)
 14
 15diff --git a/send/sftp/handler.go b/send/sftp/handler.go
 16index 9fc8e61..af0038b 100644
 17--- a/send/sftp/handler.go
 18+++ b/send/sftp/handler.go
 19@@ -10,6 +10,7 @@ import (
 20 	"slices"
 21 
 22 	"github.com/charmbracelet/ssh"
 23+	"github.com/charmbracelet/wish"
 24 	"github.com/picosh/send/send/utils"
 25 	"github.com/pkg/sftp"
 26 )
 27@@ -127,3 +128,36 @@ func (f *handler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
 28 
 29 	return reader, err
 30 }
 31+
 32+type handlererr struct {
 33+	Handler *handler
 34+}
 35+
 36+func (f *handlererr) Filecmd(r *sftp.Request) error {
 37+	err := f.Handler.Filecmd(r)
 38+	if err != nil {
 39+		wish.Errorln(f.Handler.session, err)
 40+	}
 41+	return err
 42+}
 43+func (f *handlererr) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
 44+	result, err := f.Handler.Filelist(r)
 45+	if err != nil {
 46+		wish.Errorln(f.Handler.session, err)
 47+	}
 48+	return result, err
 49+}
 50+func (f *handlererr) Filewrite(r *sftp.Request) (io.WriterAt, error) {
 51+	result, err := f.Handler.Filewrite(r)
 52+	if err != nil {
 53+		wish.Errorln(f.Handler.session, err)
 54+	}
 55+	return result, err
 56+}
 57+func (f *handlererr) Fileread(r *sftp.Request) (io.ReaderAt, error) {
 58+	result, err := f.Handler.Fileread(r)
 59+	if err != nil {
 60+		wish.Errorln(f.Handler.session, err)
 61+	}
 62+	return result, err
 63+}
 64diff --git a/send/sftp/sftp.go b/send/sftp/sftp.go
 65index b4c2003..489d443 100644
 66--- a/send/sftp/sftp.go
 67+++ b/send/sftp/sftp.go
 68@@ -5,6 +5,7 @@ import (
 69 	"io"
 70 
 71 	"github.com/charmbracelet/ssh"
 72+	"github.com/charmbracelet/wish"
 73 	"github.com/picosh/send/send/utils"
 74 	"github.com/pkg/sftp"
 75 )
 76@@ -25,19 +26,21 @@ func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHan
 77 		defer func() {
 78 			if r := recover(); r != nil {
 79 				writeHandler.GetLogger().Error("error running sftp middleware", "err", r)
 80-				_, _ = session.Stderr().Write([]byte("error running sftp middleware, check the flags you are using\r\n"))
 81+				wish.Println(session, "error running sftp middleware, check the flags you are using")
 82 			}
 83 		}()
 84 
 85 		err := writeHandler.Validate(session)
 86 		if err != nil {
 87-			utils.ErrorHandler(session, err)
 88+			wish.Errorln(session, err)
 89 			return
 90 		}
 91 
 92-		handler := &handler{
 93-			session:      session,
 94-			writeHandler: writeHandler,
 95+		handler := &handlererr{
 96+			Handler: &handler{
 97+				session:      session,
 98+				writeHandler: writeHandler,
 99+			},
100 		}
101 
102 		handlers := sftp.Handlers{
103@@ -51,6 +54,7 @@ func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHan
104 
105 		err = requestServer.Serve()
106 		if err != nil && !errors.Is(err, io.EOF) {
107+			wish.Errorln(session, err)
108 			writeHandler.GetLogger().Error("Error serving sftp subsystem", "err", err)
109 		}
110 	}
111
112base-commit: 81b1dd703a11b5b0d6defbf4177d774f8ff2b3a0
113-- 
1142.45.2
115
ps-36 by erock on 2024-08-20T03:19:05Z

refactor(sftp): print errors from server to client

Eric Bower <me@erock.io> 2024-08-20T03:16:02Z
Most clients do not print the error from request server so we need to do
it ourselves.

reference: https://github.com/pkg/sftp/issues/219#issuecomment-358136231
 send/sftp/handler.go | 34 ++++++++++++++++++++++++++++++++++
 send/sftp/sftp.go    | 14 +++++++++-----
 2 files changed, 43 insertions(+), 5 deletions(-)
  1From 5d3b1a4494cc0cf8919894785171a3c2cc60cd57 Mon Sep 17 00:00:00 2001
  2From: Eric Bower <me@erock.io>
  3Date: Mon, 19 Aug 2024 23:16:02 -0400
  4Subject: [PATCH] refactor(sftp): print errors from server to client
  5
  6Most clients do not print the error from request server so we need to do
  7it ourselves.
  8
  9reference: https://github.com/pkg/sftp/issues/219#issuecomment-358136231
 10---
 11 send/sftp/handler.go | 34 ++++++++++++++++++++++++++++++++++
 12 send/sftp/sftp.go    | 14 +++++++++-----
 13 2 files changed, 43 insertions(+), 5 deletions(-)
 14
 15diff --git a/send/sftp/handler.go b/send/sftp/handler.go
 16index 9fc8e61..af0038b 100644
 17--- a/send/sftp/handler.go
 18+++ b/send/sftp/handler.go
 19@@ -10,6 +10,7 @@ import (
 20 	"slices"
 21 
 22 	"github.com/charmbracelet/ssh"
 23+	"github.com/charmbracelet/wish"
 24 	"github.com/picosh/send/send/utils"
 25 	"github.com/pkg/sftp"
 26 )
 27@@ -127,3 +128,36 @@ func (f *handler) Fileread(r *sftp.Request) (io.ReaderAt, error) {
 28 
 29 	return reader, err
 30 }
 31+
 32+type handlererr struct {
 33+	Handler *handler
 34+}
 35+
 36+func (f *handlererr) Filecmd(r *sftp.Request) error {
 37+	err := f.Handler.Filecmd(r)
 38+	if err != nil {
 39+		wish.Errorln(f.Handler.session, err)
 40+	}
 41+	return err
 42+}
 43+func (f *handlererr) Filelist(r *sftp.Request) (sftp.ListerAt, error) {
 44+	result, err := f.Handler.Filelist(r)
 45+	if err != nil {
 46+		wish.Errorln(f.Handler.session, err)
 47+	}
 48+	return result, err
 49+}
 50+func (f *handlererr) Filewrite(r *sftp.Request) (io.WriterAt, error) {
 51+	result, err := f.Handler.Filewrite(r)
 52+	if err != nil {
 53+		wish.Errorln(f.Handler.session, err)
 54+	}
 55+	return result, err
 56+}
 57+func (f *handlererr) Fileread(r *sftp.Request) (io.ReaderAt, error) {
 58+	result, err := f.Handler.Fileread(r)
 59+	if err != nil {
 60+		wish.Errorln(f.Handler.session, err)
 61+	}
 62+	return result, err
 63+}
 64diff --git a/send/sftp/sftp.go b/send/sftp/sftp.go
 65index b4c2003..489d443 100644
 66--- a/send/sftp/sftp.go
 67+++ b/send/sftp/sftp.go
 68@@ -5,6 +5,7 @@ import (
 69 	"io"
 70 
 71 	"github.com/charmbracelet/ssh"
 72+	"github.com/charmbracelet/wish"
 73 	"github.com/picosh/send/send/utils"
 74 	"github.com/pkg/sftp"
 75 )
 76@@ -25,19 +26,21 @@ func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHan
 77 		defer func() {
 78 			if r := recover(); r != nil {
 79 				writeHandler.GetLogger().Error("error running sftp middleware", "err", r)
 80-				_, _ = session.Stderr().Write([]byte("error running sftp middleware, check the flags you are using\r\n"))
 81+				wish.Println(session, "error running sftp middleware, check the flags you are using")
 82 			}
 83 		}()
 84 
 85 		err := writeHandler.Validate(session)
 86 		if err != nil {
 87-			utils.ErrorHandler(session, err)
 88+			wish.Errorln(session, err)
 89 			return
 90 		}
 91 
 92-		handler := &handler{
 93-			session:      session,
 94-			writeHandler: writeHandler,
 95+		handler := &handlererr{
 96+			Handler: &handler{
 97+				session:      session,
 98+				writeHandler: writeHandler,
 99+			},
100 		}
101 
102 		handlers := sftp.Handlers{
103@@ -51,6 +54,7 @@ func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHan
104 
105 		err = requestServer.Serve()
106 		if err != nil && !errors.Is(err, io.EOF) {
107+			wish.Errorln(session, err)
108 			writeHandler.GetLogger().Error("Error serving sftp subsystem", "err", err)
109 		}
110 	}
111
112base-commit: 81b1dd703a11b5b0d6defbf4177d774f8ff2b3a0
113-- 
1142.45.2
115