aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorHugo Wetterberg <[email protected]>2021-01-07 23:08:50 +0100
committerAdnan Maolood <[email protected]>2021-01-09 23:53:07 -0500
commitf2921a396f954425459ccfae7b30957d778eab9d (patch)
treeaa4b5f096bb4182bf9f92841d39fdf095932c410 /client.go
parentserver: abort request handling on bad requests (diff)
downloadgo-gemini-f2921a396f954425459ccfae7b30957d778eab9d.tar.xz
go-gemini-f2921a396f954425459ccfae7b30957d778eab9d.zip
Add missing error handling
Error handling is currently missing is a couple of places. Most of them are i/o related. This change adds checks, an therefore sometimes also has to change function signatures by adding an error return value. In the case of the response writer the status and meta handling is changed and this also breaks the API. In some places where we don't have any reasonable I've added assignment to a blank identifier to make it clear that we're ignoring an error. text: read the Err() that can be set by the scanner. client: check if conn.SetDeadline() returns an error. client: check if req.Write() returns an error. fs: panic if mime type registration fails. server: stop performing i/o in Header/Status functions By deferring the actual header write to the first Write() or Flush() call we don't have to do any error handling in Header() or Status(). As Server.respond() now defers a ResponseWriter.Flush() instead of directly flushing the underlying bufio.Writer this has the added benefit of ensuring that we always write a header to the client, even if the responder is a complete NOOP. tofu: return an error if we fail to write to the known hosts writer.
Diffstat (limited to 'client.go')
-rw-r--r--client.go15
1 files changed, 13 insertions, 2 deletions
diff --git a/client.go b/client.go
index 8c34242..5afebdd 100644
--- a/client.go
+++ b/client.go
@@ -6,6 +6,7 @@ import (
"crypto/tls"
"crypto/x509"
"errors"
+ "fmt"
"net"
"strings"
"time"
@@ -74,12 +75,22 @@ func (c *Client) Do(req *Request) (*Response, error) {
conn := tls.Client(netConn, config)
// Set connection deadline
if c.Timeout != 0 {
- conn.SetDeadline(time.Now().Add(c.Timeout))
+ err := conn.SetDeadline(time.Now().Add(c.Timeout))
+ if err != nil {
+ return nil, fmt.Errorf(
+ "failed to set connection deadline: %w", err)
+ }
}
// Write the request
w := bufio.NewWriter(conn)
- req.Write(w)
+
+ err = req.Write(w)
+ if err != nil {
+ return nil, fmt.Errorf(
+ "failed to write request data: %w", err)
+ }
+
if err := w.Flush(); err != nil {
return nil, err
}