Skip to content

Commit

Permalink
wrap ALL THE ERRORS
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyak committed Mar 12, 2022
1 parent e2d7d5d commit 352415e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions chatclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewClient(connection *chatConnection, room *ChatRoom, name, color string) (
}

if err := c.setName(name); err != nil {
return nil, fmt.Errorf("could not set client name to %#v: %v", name, err)
return nil, fmt.Errorf("could not set client name to %#v: %w", name, err)
}

// Set initial vaules to their rate limit duration in the past.
Expand Down Expand Up @@ -185,29 +185,29 @@ func (cl *Client) SendChatData(data common.ChatData) error {
var err error
data = cl.replaceColorizedName(data)
if err != nil {
return fmt.Errorf("could not colorize name: %v", err)
return fmt.Errorf("could not colorize name: %w", err)
}
}

cd, err := data.ToJSON()
if err != nil {
return fmt.Errorf("could not create ChatDataJSON of type %d: %v", data.Type, err)
return fmt.Errorf("could not create ChatDataJSON of type %d: %w", data.Type, err)
}
return cl.Send(cd)
}

func (cl *Client) Send(data common.ChatDataJSON) error {
err := cl.conn.WriteData(data)
if err != nil {
return fmt.Errorf("could not send message: %v", err)
return fmt.Errorf("could not send message: %w", err)
}
return nil
}

func (cl *Client) SendServerMessage(s string) error {
err := cl.SendChatData(common.NewChatMessage("", ColorServerMessage, s, common.CmdlUser, common.MsgServer))
if err != nil {
return fmt.Errorf("could send server message to %s: message - %#v: %v", cl.name, s, err)
return fmt.Errorf("could send server message to %s: message - %#v: %w", cl.name, s, err)
}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions chatroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newChatRoom() (*ChatRoom, error) {

err := loadEmotes()
if err != nil {
return nil, fmt.Errorf("error loading emotes: %s", err)
return nil, fmt.Errorf("error loading emotes: %w", err)
}
common.LogInfof("Loaded %d emotes\n", len(common.Emotes))

Expand Down Expand Up @@ -85,7 +85,7 @@ func (cr *ChatRoom) Join(conn *chatConnection, data common.JoinData) (*Client, e
client, err := NewClient(conn, cr, data.Name, data.Color)
if err != nil {
sendHiddenMessage(common.CdNotify, "Could not join client")
return nil, fmt.Errorf("unable to join client: %v", err)
return nil, fmt.Errorf("unable to join client: %w", err)
}

// Overwrite to use client instead
Expand Down Expand Up @@ -461,7 +461,7 @@ func (cr *ChatRoom) changeName(oldName, newName string, forced bool) error {
if currentClient != nil {
err := currentClient.setName(newName)
if err != nil {
return fmt.Errorf("could not set client name to %#v: %v", newName, err)
return fmt.Errorf("could not set client name to %#v: %w", newName, err)
}
common.LogDebugf("%q -> %q\n", oldName, newName)

Expand Down
8 changes: 4 additions & 4 deletions common/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func SetupLogging(level LogLevel, file string) error {
} else {
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open log file for writing: %s", err)
return fmt.Errorf("unable to open log file for writing: %w", err)
}
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags)
Expand All @@ -57,7 +57,7 @@ func SetupLogging(level LogLevel, file string) error {
} else {
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open log file for writing: %s", err)
return fmt.Errorf("unable to open log file for writing: %w", err)
}
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
logChat = log.New(io.MultiWriter(os.Stdout, f), logPrefixChat, log.LstdFlags)
Expand All @@ -73,7 +73,7 @@ func SetupLogging(level LogLevel, file string) error {
} else {
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open log file for writing: %s", err)
return fmt.Errorf("unable to open log file for writing: %w", err)
}
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
logInfo = log.New(io.MultiWriter(os.Stdout, f), logPrefixInfo, log.LstdFlags)
Expand All @@ -89,7 +89,7 @@ func SetupLogging(level LogLevel, file string) error {
} else {
f, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("unable to open log file for writing: %s", err)
return fmt.Errorf("unable to open log file for writing: %w", err)
}
logError = log.New(io.MultiWriter(os.Stderr, f), logPrefixError, log.LstdFlags)
}
Expand Down
2 changes: 1 addition & 1 deletion common/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func InitTemplates() error {
for key, files := range serverTemplateDefs {
t, err := html.ParseFiles(files...)
if err != nil {
return fmt.Errorf("unable to parse templates for %s: %v", key, err)
return fmt.Errorf("unable to parse templates for %s: %w", key, err)
}

serverTemplates[key] = t
Expand Down
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (cc *chatConnection) WriteData(data interface{}) error {
if operr, ok := err.(*net.OpError); ok {
common.LogDebugln("OpError: " + operr.Err.Error())
}
return fmt.Errorf("Error writing data to %s %s: %v", cc.clientName, cc.Host(), err)
return fmt.Errorf("Error writing data to %s %s: %w", cc.clientName, cc.Host(), err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion emotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func findEmotes(dir string, em common.EmotesMap) (common.EmotesMap, error) {
common.LogDebugf("finding emotes in %q\n", dir)
emotePNGs, err := filepath.Glob(filepath.Join(dir, "*.png"))
if err != nil {
return em, fmt.Errorf("unable to glob emote directory: %s", err)
return em, fmt.Errorf("unable to glob emote directory: %w", err)
}
common.LogInfof("Found %d emotePNGs\n", len(emotePNGs))

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func setupSettings() error {
var err error
settings, err = LoadSettings(confFile)
if err != nil {
return fmt.Errorf("unable to load settings: %s", err)
return fmt.Errorf("unable to load settings: %w", err)
}
if len(settings.StreamKey) == 0 {
return fmt.Errorf("missing stream key is settings.json")
Expand Down
14 changes: 7 additions & 7 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,20 @@ type BanInfo struct {
func LoadSettings(filename string) (*Settings, error) {
raw, err := ioutil.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading file: %s", err)
return nil, fmt.Errorf("error reading file: %w", err)
}

var s *Settings
err = json.Unmarshal(raw, &s)
if err != nil {
return nil, fmt.Errorf("error unmarshaling: %s", err)
return nil, fmt.Errorf("error unmarshaling: %w", err)
}
s.filename = filename

var logFileDir string = s.LogFile
fmt.Printf("Log file: %s\n", logFileDir)
if err = common.SetupLogging(s.LogLevel, logFileDir); err != nil {
return nil, fmt.Errorf("unable to setup logger: %s", err)
return nil, fmt.Errorf("unable to setup logger: %w", err)
}

// have a default of 200
Expand All @@ -101,7 +101,7 @@ func LoadSettings(filename string) (*Settings, error) {
if s.RegenAdminPass || s.AdminPassword == "" {
s.AdminPassword, err = generatePass(time.Now().Unix())
if err != nil {
return nil, fmt.Errorf("unable to generate admin password: %s", err)
return nil, fmt.Errorf("unable to generate admin password: %w", err)
}
}

Expand Down Expand Up @@ -184,7 +184,7 @@ func LoadSettings(filename string) (*Settings, error) {

// Save admin password to file
if err = s.Save(); err != nil {
return nil, fmt.Errorf("unable to save settings: %s", err)
return nil, fmt.Errorf("unable to save settings: %w", err)
}

return s, nil
Expand Down Expand Up @@ -214,12 +214,12 @@ func (s *Settings) Save() error {
func (s *Settings) unlockedSave() error {
marshaled, err := json.MarshalIndent(s, "", "\t")
if err != nil {
return fmt.Errorf("error marshaling: %s", err)
return fmt.Errorf("error marshaling: %w", err)
}

err = ioutil.WriteFile(s.filename, marshaled, 0777)
if err != nil {
return fmt.Errorf("error saving: %s", err)
return fmt.Errorf("error saving: %w", err)
}
return nil
}
Expand Down

0 comments on commit 352415e

Please sign in to comment.