Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling. #75

Merged
merged 1 commit into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func instantiate(config *Config) {
fmt.Printf("Connecting to %s.", config.Server)
conn := irc.New(config.UserName, config.Version)
config.irc = conn
core.Join(conn, config.Server)
err := core.Join(conn, config.Server)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%sConnected to %s.\n", clearLine, config.Server)
}

Expand Down
8 changes: 6 additions & 2 deletions core/irchighway.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import (
// Specific irc.irchighway.net commands

// Join connects to the irc.irchighway.net server and joins the #ebooks channel
func Join(irc *irc.Conn, url string) {
irc.Connect(url)
func Join(irc *irc.Conn, url string) error {
err := irc.Connect(url)
if err != nil {
return err
}
// Wait before joining the ebooks room
// Often you recieve a private message from the server
time.Sleep(time.Second * 2)
irc.JoinChannel("ebooks")
return nil
}

// SearchBook sends a search query to the search bot
Expand Down
8 changes: 4 additions & 4 deletions core/search_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -64,14 +63,15 @@ func (p ParseError) String() string {
}

// ParseSearchFile converts a single search file into an array of BookDetail
func ParseSearchFile(filePath string) ([]BookDetail, []ParseError) {
func ParseSearchFile(filePath string) ([]BookDetail, []ParseError, error) {
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
return nil, nil, err
}
defer file.Close()

return ParseSearchV2(file)
books, errs := ParseSearchV2(file)
return books, errs, nil
}

func ParseSearch(reader io.Reader) ([]BookDetail, []ParseError) {
Expand Down
7 changes: 3 additions & 4 deletions dcc/dcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"errors"
"io"
"log"
"net"
"regexp"
"strconv"
Expand Down Expand Up @@ -76,13 +75,13 @@ func (download Download) Download(writer io.Writer) error {
bytes := make([]byte, 4096)
for int64(received) < download.Size {
n, err := conn.Read(bytes)

if err != nil {
log.Fatal("Error Downloading Data", err)
return err
}

_, err = writer.Write(bytes[:n])
if err != nil {
log.Println(err)
return err
}
received += n
}
Expand Down
30 changes: 22 additions & 8 deletions irc/irc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package irc

import (
"log"
"net"
)

Expand All @@ -25,11 +24,10 @@ func New(username, realname string) *Conn {
}

// Connect connects to the given server at port 6667
func (i *Conn) Connect(address string) {
func (i *Conn) Connect(address string) error {
conn, err := net.Dial("tcp", address+":6667")

if err != nil {
log.Fatal("IRC Connection Error ", err)
return err
}

i.Conn = conn
Expand All @@ -39,40 +37,56 @@ func (i *Conn) Connect(address string) {

i.Write([]byte(user))
i.Write([]byte(nick))
return nil
}

// Disconnect closes connection to the IRC server
func (i *Conn) Disconnect() {
if !i.IsConnected() {
return
}
i.Write([]byte("QUIT :Goodbye\r\n"))
i.Conn.Close()
}

// SendMessage sends the given message string to the connected IRC server
func (i *Conn) SendMessage(message string) {
if !i.IsConnected() {
return
}
i.Write([]byte("PRIVMSG #" + i.channel + " :" + message + "\r\n"))
}

// SendNotice sends a notice message to the specified user
func (i *Conn) SendNotice(user string, message string) {
if !i.IsConnected() {
return
}
i.Write([]byte("NOTICE " + user + " :" + message + "\r\n"))
}

// JoinChannel joins the channel given by channel string
func (i *Conn) JoinChannel(channel string) {
i.channel = channel
_, err := i.Write([]byte("JOIN #" + channel + "\r\n"))
if err != nil {
log.Fatal(err)
if !i.IsConnected() {
return
}
i.channel = channel
i.Write([]byte("JOIN #" + channel + "\r\n"))
}

// GetUsers sends a NAMES request to the IRC server
func (i *Conn) GetUsers(channel string) {
if !i.IsConnected() {
return
}
i.Write([]byte("NAMES #" + channel + "\r\n"))
}

// Pong sends a Pong message to the server, often used after a PING request
func (i *Conn) Pong(server string) {
if !i.IsConnected() {
return
}
i.Write([]byte("PONG " + server + "\r\n"))
}

Expand Down
31 changes: 19 additions & 12 deletions server/irc_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,32 @@ func (c *Client) searchResultHandler(downloadDir string) core.HandlerFunc {
extractedPath, err := core.DownloadExtractDCCString(filepath.Join(downloadDir, "books"), text, nil)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Error when downloading search results.")
return
}

results, errors := core.ParseSearchFile(extractedPath)
// Output all errors so parser can be improved over time
if len(errors) > 0 {
c.log.Printf("%d Search Result Parsing Errors\n", len(errors))
for _, err := range errors {
c.log.Println(err)
}
bookResults, parseErrors, err := core.ParseSearchFile(extractedPath)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Error when parsing search results.")
return
}

if len(results) == 0 && len(errors) == 0 {
if len(bookResults) == 0 && len(parseErrors) == 0 {
c.noResultsHandler(text)
return
}

c.log.Printf("Sending %d search results.\n", len(results))
c.send <- newSearchResponse(results, errors)
// Output all errors so parser can be improved over time
if len(parseErrors) > 0 {
c.log.Printf("%d Search Result Parsing Errors\n", len(parseErrors))
for _, err := range parseErrors {
c.log.Println(err)
}
}

c.log.Printf("Sending %d search results.\n", len(bookResults))
c.send <- newSearchResponse(bookResults, parseErrors)

err = os.Remove(extractedPath)
if err != nil {
Expand All @@ -60,8 +68,7 @@ func (c *Client) bookResultHandler(downloadDir string) core.HandlerFunc {
extractedPath, err := core.DownloadExtractDCCString(filepath.Join(downloadDir, "books"), text, nil)
if err != nil {
c.log.Println(err)

c.send <- newErrorResponse(err.Error())
c.send <- newErrorResponse("Error when downloading book.")
return
}

Expand Down
2 changes: 1 addition & 1 deletion server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (server *server) staticFilesHandler(assetPath string) http.Handler {
// update the embedded file system's tree so that index.html is at the root
app, err := fs.Sub(reactClient, assetPath)
if err != nil {
server.log.Fatal(err)
server.log.Println(err)
}

// strip the predefined base path and serve the static file
Expand Down
8 changes: 7 additions & 1 deletion server/websocket_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ func (server *server) routeMessage(message Request, c *Client) {

// handle ConnectionRequests and either connect to the server or do nothing
func (c *Client) startIrcConnection(server *server) {
core.Join(c.irc, server.config.Server)
err := core.Join(c.irc, server.config.Server)
if err != nil {
c.log.Println(err)
c.send <- newErrorResponse("Unable to connect to IRC server.")
return
}

handler := server.NewIrcEventHandler(c)

if server.config.Log {
Expand Down