Skip to content

Commit

Permalink
ssh: Consistent error handling in examples
Browse files Browse the repository at this point in the history
After discussion around an example SFTP implementation:
pkg/sftp#54
it has been suggested that errors should be handled using
log.Fatal rather than panic, and that the actual underlying error
should also be logged. In the existing SSH examples there
are several different styles of error handling using both panic
and log.Fatalf.

This patch uses log.Fatal consistently for all of these cases.

Change-Id: I2cebfae1821530dc3c5bbc46d451fe026bed582f
Reviewed-on: https://go-review.googlesource.com/16736
Reviewed-by: Russ Cox <rsc@golang.org>
  • Loading branch information
kothar authored and rsc committed Oct 3, 2016
1 parent 87591fa commit 57c4836
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions ssh/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func ExampleNewServerConn() {

privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
log.Fatal("Failed to load private key: ", err)
}

private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
log.Fatal("Failed to parse private key: ", err)
}

config.AddHostKey(private)
Expand All @@ -46,18 +46,18 @@ func ExampleNewServerConn() {
// accepted.
listener, err := net.Listen("tcp", "0.0.0.0:2022")
if err != nil {
panic("failed to listen for connection")
log.Fatal("failed to listen for connection: ", err)
}
nConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
log.Fatal("failed to accept incoming connection: ", err)
}

// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
log.Fatal("failed to handshake: ", err)
}
// The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs)
Expand All @@ -74,7 +74,7 @@ func ExampleNewServerConn() {
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
log.Fatal("could not accept channel: ", err)
}

// Sessions have out-of-band requests such as "shell",
Expand Down Expand Up @@ -125,14 +125,14 @@ func ExampleDial() {
}
client, err := ssh.Dial("tcp", "yourserver.com:22", config)
if err != nil {
panic("Failed to dial: " + err.Error())
log.Fatal("Failed to dial: ", err)
}

// Each ClientConn can support multiple interactive sessions,
// represented by a Session.
session, err := client.NewSession()
if err != nil {
panic("Failed to create session: " + err.Error())
log.Fatal("Failed to create session: ", err)
}
defer session.Close()

Expand All @@ -141,7 +141,7 @@ func ExampleDial() {
var b bytes.Buffer
session.Stdout = &b
if err := session.Run("/usr/bin/whoami"); err != nil {
panic("Failed to run: " + err.Error())
log.Fatal("Failed to run: " + err.Error())
}
fmt.Println(b.String())
}
Expand Down Expand Up @@ -189,14 +189,14 @@ func ExampleClient_Listen() {
// Dial your ssh server.
conn, err := ssh.Dial("tcp", "localhost:22", config)
if err != nil {
log.Fatalf("unable to connect: %s", err)
log.Fatal("unable to connect: ", err)
}
defer conn.Close()

// Request the remote side to open port 8080 on all interfaces.
l, err := conn.Listen("tcp", "0.0.0.0:8080")
if err != nil {
log.Fatalf("unable to register tcp forward: %v", err)
log.Fatal("unable to register tcp forward: ", err)
}
defer l.Close()

Expand All @@ -217,13 +217,13 @@ func ExampleSession_RequestPty() {
// Connect to ssh server
conn, err := ssh.Dial("tcp", "localhost:22", config)
if err != nil {
log.Fatalf("unable to connect: %s", err)
log.Fatal("unable to connect: ", err)
}
defer conn.Close()
// Create a session
session, err := conn.NewSession()
if err != nil {
log.Fatalf("unable to create session: %s", err)
log.Fatal("unable to create session: ", err)
}
defer session.Close()
// Set up terminal modes
Expand All @@ -234,10 +234,10 @@ func ExampleSession_RequestPty() {
}
// Request pseudo terminal
if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
log.Fatalf("request for pseudo terminal failed: %s", err)
log.Fatal("request for pseudo terminal failed: ", err)
}
// Start remote shell
if err := session.Shell(); err != nil {
log.Fatalf("failed to start shell: %s", err)
log.Fatal("failed to start shell: ", err)
}
}

0 comments on commit 57c4836

Please sign in to comment.