Browse Source

Better error logging

master
Gregory Rudolph 3 years ago
parent
commit
610ea960f1
Signed by: rudi
GPG Key ID: EF64F3CBD1A1EBDD
  1. 31
      commands.go
  2. 3
      main.go

31
commands.go

@ -14,12 +14,12 @@ import ( @@ -14,12 +14,12 @@ import (
func reset(m chat1.MsgSummary) {
_, err := k.KVDelete(&m.Channel.Name, "teslabot", "authtok")
if err != nil {
handleError(err,m,"There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
return
}
_, err = k.KVDelete(&m.Channel.Name, "teslabot", "startPass")
if err != nil {
handleError(err,m,"There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error resetting your authentication. Contact @rudi9719 for more information with code %+v")
return
}
}
@ -42,13 +42,13 @@ func authenticate(m chat1.MsgSummary) { @@ -42,13 +42,13 @@ func authenticate(m chat1.MsgSummary) {
password := parts[2]
t, err := login(context.Background(), username, password)
if err != nil {
handleError(err,m,"There was an error logging in. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error logging in. Contact @rudi9719 for more information with code %+v")
return
}
log.LogDebug("Token created for %+v", m.Sender.Username)
_, err = k.KVPut(&m.Channel.Name, "teslabot", "authtok", t)
if err != nil {
handleError(err,m,"There was an error storing your auth token. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error storing your auth token. Contact @rudi9719 for more information with code %+v")
return
}
k.ReactByConvID(m.ConvID, m.Id, ":car:")
@ -63,7 +63,7 @@ func listVehicles(m chat1.MsgSummary) { @@ -63,7 +63,7 @@ func listVehicles(m chat1.MsgSummary) {
}
v, err := c.Vehicles()
if err != nil {
handleError(err,m,"There was an error listing vehicles. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error listing vehicles. Contact @rudi9719 for more information with code %+v")
return
}
ret := "Detected vehicles for account: ```"
@ -83,7 +83,7 @@ func deferTime(m chat1.MsgSummary) { @@ -83,7 +83,7 @@ func deferTime(m chat1.MsgSummary) {
m.Content.Text.Body = fmt.Sprintf("!%+v", command)
timer, err := time.ParseDuration(t)
if err != nil {
handleError(err,m,"There was an error parsing your time input. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error parsing your time input. Contact @rudi9719 for more information with code %+v")
return
}
k.SendMessageByConvID(m.ConvID, fmt.Sprintf("I will run `%+v` at %+v", command, time.Now().Add(timer).Format("Jan _2 15:04:05")))
@ -99,7 +99,7 @@ func honk(m chat1.MsgSummary) { @@ -99,7 +99,7 @@ func honk(m chat1.MsgSummary) {
}
err := v.HonkHorn()
if err != nil {
handleError(err, m,"There was an error honking your horn. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error honking your horn. Contact @rudi9719 for more information with code %+v")
return
}
k.SendMessageByConvID(m.ConvID, "I've honked your horn!")
@ -112,7 +112,7 @@ func chargeStatus(m chat1.MsgSummary) { @@ -112,7 +112,7 @@ func chargeStatus(m chat1.MsgSummary) {
}
state, err := v.ChargeState()
if err != nil {
handleError(err,m,"There was an error getting charge state. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error getting charge state. Contact @rudi9719 for more information with code %+v")
return
}
ret := fmt.Sprintf("Status for %+v: ```", v.DisplayName)
@ -144,7 +144,7 @@ func flashLights(m chat1.MsgSummary) { @@ -144,7 +144,7 @@ func flashLights(m chat1.MsgSummary) {
}
err := v.FlashLights()
if err != nil {
handleError(err,m,"There was an error flashing your lights. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error flashing your lights. Contact @rudi9719 for more information with code %+v")
return
}
@ -158,7 +158,7 @@ func currentTemp(m chat1.MsgSummary) { @@ -158,7 +158,7 @@ func currentTemp(m chat1.MsgSummary) {
}
guiSettings, err := v.GuiSettings()
if err != nil {
handleError(err,m,"There was an error getting your preferences. Contact @rudi9719 for more information with code %+v")
handleError(err, m, "There was an error getting your preferences. Contact @rudi9719 for more information with code %+v")
return
}
climateState, err := v.ClimateState()
@ -388,13 +388,16 @@ func handleError(err error, m chat1.MsgSummary, msg string) { @@ -388,13 +388,16 @@ func handleError(err error, m chat1.MsgSummary, msg string) {
return
}
if strings.HasPrefix(err.Error(), "408") {
k.SendMessageByConvID(m.ConvID, "Unable to wake vehicle within the timeframe. Trying to run the command again.")
handleChat(m)
return
if !m.Content.Attachment.Uploaded {
k.SendMessageByConvID(m.ConvID, "Unable to wake vehicle within the timeframe. Trying to run the command again.")
m.Content.Attachment.Uploaded = true
handleChat(m)
return
}
}
if strings.HasPrefix(err.Error(), "400") {
k.SendMessageByConvID(m.ConvID, "Tesla returned an error. The command you tried to use may need an update. Please contact @rudi9719 with tracking ID %+v and the bad command.", tracker)
return
}
k.SendMessageByConvID(m.ConvID, msg, tracker)
}
}

3
main.go

@ -44,6 +44,9 @@ func main() { @@ -44,6 +44,9 @@ func main() {
func handleChat(m chat1.MsgSummary) {
defer log.PanicSafe()
if m.Content.TypeName != "text" {
return
}
parts := strings.Split(m.Content.Text.Body, " ")
command := strings.Replace(parts[0], "!", "", -1)
for _, v := range commands {

Loading…
Cancel
Save