From f2921a396f954425459ccfae7b30957d778eab9d Mon Sep 17 00:00:00 2001 From: Hugo Wetterberg Date: Thu, 7 Jan 2021 23:08:50 +0100 Subject: 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. --- text.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'text.go') diff --git a/text.go b/text.go index 9b77982..6263d93 100644 --- a/text.go +++ b/text.go @@ -88,17 +88,17 @@ func (l LineText) line() {} type Text []Line // ParseText parses Gemini text from the provided io.Reader. -func ParseText(r io.Reader) Text { +func ParseText(r io.Reader) (Text, error) { var t Text - ParseLines(r, func(line Line) { + err := ParseLines(r, func(line Line) { t = append(t, line) }) - return t + return t, err } // ParseLines parses Gemini text from the provided io.Reader. // It calls handler with each line that it parses. -func ParseLines(r io.Reader, handler func(Line)) { +func ParseLines(r io.Reader, handler func(Line)) error { const spacetab = " \t" var pre bool scanner := bufio.NewScanner(r) @@ -149,6 +149,8 @@ func ParseLines(r io.Reader, handler func(Line)) { } handler(line) } + + return scanner.Err() } // String writes the Gemini text response to a string and returns it. -- cgit v1.2.3