Browse Source

add more details to error events.

master
Gisle Aune 4 years ago
parent
commit
5b3c0b6541
  1. 12
      client.go
  2. 17
      event_error.go
  3. 2
      handlers/ctcp.go
  4. 16
      handlers/input.go
  5. 8
      handlers/mroleplay.go

12
client.go

@ -255,7 +255,7 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
}) })
if err != nil { if err != nil {
if !client.Destroyed() { if !client.Destroyed() {
client.EmitNonBlocking(NewErrorEvent("connect", "Connect failed: "+err.Error()))
client.EmitNonBlocking(NewErrorEvent("connect", "TLS connect failed: "+err.Error(), "connect_failed_tls", err))
} }
return err return err
} }
@ -263,7 +263,7 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
conn, err = net.Dial("tcp", addr) conn, err = net.Dial("tcp", addr)
if err != nil { if err != nil {
if !client.Destroyed() { if !client.Destroyed() {
client.EmitNonBlocking(NewErrorEvent("connect", "Connect failed: "+err.Error()))
client.EmitNonBlocking(NewErrorEvent("connect", "Connect failed: "+err.Error(), "connect_failed", err))
} }
return err return err
} }
@ -283,7 +283,7 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
for { for {
line, err := reader.ReadString('\n') line, err := reader.ReadString('\n')
if err != nil { if err != nil {
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: "+err.Error()))
client.EmitNonBlocking(NewErrorEvent("read", "Read failed: "+err.Error(), "read_failed", err))
break break
} }
line = replacer.Replace(line) line = replacer.Replace(line)
@ -295,7 +295,7 @@ func (client *Client) Connect(addr string, ssl bool) (err error) {
client.mutex.RUnlock() client.mutex.RUnlock()
if !hasQuit { if !hasQuit {
client.EmitNonBlocking(NewErrorEvent("parse", "Read failed: "+err.Error()))
client.EmitNonBlocking(NewErrorEvent("parse", "Parse failed: "+err.Error(), "parse_failed", err))
} }
continue continue
} }
@ -366,7 +366,7 @@ func (client *Client) Send(line string) error {
_ = conn.SetWriteDeadline(time.Now().Add(time.Second * 30)) _ = conn.SetWriteDeadline(time.Now().Add(time.Second * 30))
_, err := conn.Write([]byte(line)) _, err := conn.Write([]byte(line))
if err != nil { if err != nil {
client.EmitNonBlocking(NewErrorEvent("write", err.Error()))
client.EmitNonBlocking(NewErrorEvent("write", "Write failed:"+err.Error(), "connect_failed", err))
_ = client.Disconnect(false) _ = client.Disconnect(false)
} }
@ -508,7 +508,7 @@ func (client *Client) EmitInput(line string, target Target) context.Context {
client.mutex.RLock() client.mutex.RLock()
if target != nil && client.TargetByID(target.ID()) == nil { if target != nil && client.TargetByID(target.ID()) == nil {
client.EmitNonBlocking(NewErrorEvent("invalid_target", "Target does not exist."))
client.EmitNonBlocking(NewErrorEvent("target", "Target does not exist.", "target_do_not_exist", nil))
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()

17
event_error.go

@ -2,9 +2,24 @@ package irc
// NewErrorEvent makes an event of kind `error` and verb `code` with the text. // NewErrorEvent makes an event of kind `error` and verb `code` with the text.
// It's absolutely trivial, but it's good to have standarized. // It's absolutely trivial, but it's good to have standarized.
func NewErrorEvent(code, text string) Event {
func NewErrorEvent(code, text, i18nKey string, raw error) Event {
return NewErrorEventTarget(nil, code, text, i18nKey, raw)
}
func NewErrorEventTarget(target Target, code, text, i18nKey string, raw error) Event {
event := NewEvent("error", code) event := NewEvent("error", code)
event.Text = text event.Text = text
if target != nil {
event.targets = append(event.targets, target)
}
if i18nKey != "" {
event.Tags["i18n_key"] = i18nKey
}
if raw != nil {
event.Tags["raw"] = raw.Error()
}
return event return event
} }

2
handlers/ctcp.go

@ -52,7 +52,7 @@ func CTCP(event *irc.Event, client *irc.Client) {
args := strings.SplitN(event.Text, " ", 2) args := strings.SplitN(event.Text, " ", 2)
targetName := args[0] targetName := args[0]
if targetName == "" { if targetName == "" {
client.EmitNonBlocking(irc.NewErrorEvent("ctcp.pingarg", "/ping needs an argument"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "/ping needs an argument", "usage_ping", nil))
break break
} }

16
handlers/input.go

@ -17,7 +17,7 @@ func Input(event *irc.Event, client *irc.Client) {
targetName, text := ircutil.ParseArgAndText(event.Text) targetName, text := ircutil.ParseArgAndText(event.Text)
if targetName == "" || text == "" { if targetName == "" || text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /msg <target> <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /msg <target> <text...>", "usage_msg", nil))
break break
} }
@ -34,13 +34,13 @@ func Input(event *irc.Event, client *irc.Client) {
event.PreventDefault() event.PreventDefault()
if event.Text == "" { if event.Text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /text <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /text <text...>", "usage_text", nil))
break break
} }
target := event.Target("query", "channel") target := event.Target("query", "channel")
if target == nil { if target == nil {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel or query"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Target is not a channel or query", "target_not_channel_or_query", nil))
break break
} }
@ -57,13 +57,13 @@ func Input(event *irc.Event, client *irc.Client) {
event.PreventDefault() event.PreventDefault()
if event.Text == "" { if event.Text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /me <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /me <text...>", "usage_me", nil))
break break
} }
target := event.Target("query", "channel") target := event.Target("query", "channel")
if target == nil { if target == nil {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel or query"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Target is not a channel or query", "target_not_channel_or_query", nil))
break break
} }
@ -93,7 +93,7 @@ func Input(event *irc.Event, client *irc.Client) {
targetName, text := ircutil.ParseArgAndText(event.Text) targetName, text := ircutil.ParseArgAndText(event.Text)
if targetName == "" || text == "" { if targetName == "" || text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /describe <target> <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /describe <target> <text...>", "usage_describe", nil))
break break
} }
@ -122,7 +122,7 @@ func Input(event *irc.Event, client *irc.Client) {
event.PreventDefault() event.PreventDefault()
if event.Text == "" { if event.Text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /m <modes and args...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /m <modes and args...>", "usage_m", nil))
break break
} }
@ -131,7 +131,7 @@ func Input(event *irc.Event, client *irc.Client) {
} else if status := event.StatusTarget(); status != nil { } else if status := event.StatusTarget(); status != nil {
client.SendQueuedf("MODE %s %s", client.Nick(), event.Text) client.SendQueuedf("MODE %s %s", client.Nick(), event.Text)
} else { } else {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel or status"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Target is not a channel or status", "channel_not_channel_or_status", nil))
} }
} }

8
handlers/mroleplay.go

@ -70,13 +70,13 @@ func MRoleplay(event *irc.Event, client *irc.Client) {
isAction := event.Verb() == "npcac" isAction := event.Verb() == "npcac"
nick, text := ircutil.ParseArgAndText(event.Text) nick, text := ircutil.ParseArgAndText(event.Text)
if nick == "" || text == "" { if nick == "" || text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /"+event.Verb()+" <nick> <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /"+event.Verb()+" <nick> <text...>", "usage_"+event.Verb(), nil))
break break
} }
channel := event.ChannelTarget() channel := event.ChannelTarget()
if channel == nil { if channel == nil {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Target is not a channel", "target_not_channel", nil))
break break
} }
@ -99,13 +99,13 @@ func MRoleplay(event *irc.Event, client *irc.Client) {
case "input.scenec", "input.narratorc": case "input.scenec", "input.narratorc":
{ {
if event.Text == "" { if event.Text == "" {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Usage: /"+event.Verb()+" <text...>"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Usage: /"+event.Verb()+" <text...>", "usage_"+event.Verb(), nil))
break break
} }
channel := event.ChannelTarget() channel := event.ChannelTarget()
if channel == nil { if channel == nil {
client.EmitNonBlocking(irc.NewErrorEvent("input", "Target is not a channel"))
client.EmitNonBlocking(irc.NewErrorEventTarget(event.Target(), "input", "Target is not a channel", "target_not_channel", nil))
break break
} }

Loading…
Cancel
Save