cmd/tsconnect: output errors to the JS console too

We were just outputting them to the terminal, but that's hard to debug
because we immediately tear down the terminal when getting an error.

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
pull/5498/head
Mihai Parparita 2 years ago committed by Mihai Parparita
parent 122bd667dc
commit 27f36f77c3

@ -27,26 +27,39 @@ export function runSSHSession(
term.focus() term.focus()
const sshSession = ipn.ssh(def.hostname, def.username, { let resizeObserver: ResizeObserver | undefined
writeFn: (input) => term.write(input), let handleBeforeUnload: ((e: BeforeUnloadEvent) => void) | undefined
setReadFn: (hook) => (onDataHook = hook),
const sshSession = ipn.ssh(def.hostname + "2", def.username, {
writeFn(input) {
term.write(input)
},
writeErrorFn(err) {
console.error(err)
term.write(err)
},
setReadFn(hook) {
onDataHook = hook
},
rows: term.rows, rows: term.rows,
cols: term.cols, cols: term.cols,
onDone: () => { onDone() {
resizeObserver.disconnect() resizeObserver?.disconnect()
term.dispose() term.dispose()
window.removeEventListener("beforeunload", handleBeforeUnload) if (handleBeforeUnload) {
window.removeEventListener("beforeunload", handleBeforeUnload)
}
onDone() onDone()
}, },
}) })
// Make terminal and SSH session track the size of the containing DOM node. // Make terminal and SSH session track the size of the containing DOM node.
const resizeObserver = new ResizeObserver(() => fitAddon.fit()) resizeObserver = new ResizeObserver(() => fitAddon.fit())
resizeObserver.observe(termContainerNode) resizeObserver.observe(termContainerNode)
term.onResize(({ rows, cols }) => sshSession.resize(rows, cols)) term.onResize(({ rows, cols }) => sshSession.resize(rows, cols))
// Close the session if the user closes the window without an explicit // Close the session if the user closes the window without an explicit
// exit. // exit.
const handleBeforeUnload = () => sshSession.close() handleBeforeUnload = () => sshSession.close()
window.addEventListener("beforeunload", handleBeforeUnload) window.addEventListener("beforeunload", handleBeforeUnload)
} }

@ -19,6 +19,7 @@ declare global {
username: string, username: string,
termConfig: { termConfig: {
writeFn: (data: string) => void writeFn: (data: string) => void
writeErrorFn: (err: string) => void
setReadFn: (readFn: (data: string) => void) => void setReadFn: (readFn: (data: string) => void) => void
rows: number rows: number
cols: number cols: number

@ -347,6 +347,7 @@ type jsSSHSession struct {
func (s *jsSSHSession) Run() { func (s *jsSSHSession) Run() {
writeFn := s.termConfig.Get("writeFn") writeFn := s.termConfig.Get("writeFn")
writeErrorFn := s.termConfig.Get("writeErrorFn")
setReadFn := s.termConfig.Get("setReadFn") setReadFn := s.termConfig.Get("setReadFn")
rows := s.termConfig.Get("rows").Int() rows := s.termConfig.Get("rows").Int()
cols := s.termConfig.Get("cols").Int() cols := s.termConfig.Get("cols").Int()
@ -357,7 +358,7 @@ func (s *jsSSHSession) Run() {
writeFn.Invoke(s) writeFn.Invoke(s)
} }
writeError := func(label string, err error) { writeError := func(label string, err error) {
write(fmt.Sprintf("%s Error: %v\r\n", label, err)) writeErrorFn.Invoke(fmt.Sprintf("%s Error: %v\r\n", label, err))
} }
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

Loading…
Cancel
Save