Added some error messages.

This commit is contained in:
Martin Dosch 2019-05-31 09:06:36 +02:00
parent c111da9c8c
commit d025df0507
2 changed files with 31 additions and 31 deletions

View file

@ -56,14 +56,14 @@ func main() {
if _, err := os.Stat(configPath); os.IsNotExist(err) { if _, err := os.Stat(configPath); os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0700) err = os.MkdirAll(configPath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create config path: ", err)
} }
} }
} else { // Get the current user. } else { // Get the current user.
curUser, err := user.Current() curUser, err := user.Current()
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't get current user: ", err)
return return
} }
// Get home directory. // Get home directory.
@ -79,7 +79,7 @@ func main() {
if _, err := os.Stat(configPath + "config.json"); os.IsNotExist(err) { if _, err := os.Stat(configPath + "config.json"); os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0700) err = os.MkdirAll(configPath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create config path: ", err)
} }
} }
@ -89,7 +89,7 @@ func main() {
// Check that config file is existing. // Check that config file is existing.
if _, err := os.Stat(configFile); os.IsNotExist(err) { if _, err := os.Stat(configFile); os.IsNotExist(err) {
log.Fatal("Error: ", err) log.Fatal("Error: Can't find config file: ", err)
} }
// Read configuration file into variable config. // Read configuration file into variable config.
@ -98,13 +98,13 @@ func main() {
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
config := configuration{} config := configuration{}
if err := decoder.Decode(&config); err != nil { if err := decoder.Decode(&config); err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't decode config: ", err)
} }
if _, err := os.Stat(configFile); os.IsNotExist(err) { if _, err := os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(configPath, 0700) err = os.MkdirAll(configPath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create config path: ", err)
} }
} }
@ -122,13 +122,13 @@ func main() {
// Connect to server. // Connect to server.
client, err = options.NewClient() client, err = options.NewClient()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Can't connect to xmpp server: ", err)
} }
// Join the MUC // Join the MUC
_, err := client.JoinMUCNoHistory(config.Muc, config.MucNick) _, err := client.JoinMUCNoHistory(config.Muc, config.MucNick)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Can't join MUC: ", err)
} }
// Starting goroutine to ping the server every 30 seconds. // Starting goroutine to ping the server every 30 seconds.
@ -152,7 +152,7 @@ func main() {
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt) output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt)
if err != nil { if err != nil {
// Exit if an error occurs checking the feeds. // Exit if an error occurs checking the feeds.
log.Fatal(err) log.Fatal("Error: Can't check feeds for new articles: ", err)
} }
if output != "" { if output != "" {
@ -161,7 +161,7 @@ func main() {
if err != nil { if err != nil {
// ToDo: Save message for resend. // ToDo: Save message for resend.
// Exit if message can not be sent. // Exit if message can not be sent.
log.Fatal(err) log.Fatal("Error: Can't send message to MUC: ", err)
} }
} }
} }
@ -179,7 +179,7 @@ func pingMUC(client *xmpp.Client, botJid string, muc string, mucNick string) {
id, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(), id, err = client.RawInformation(botJid, muc+"/"+mucNick, sid.Id(),
"get", "<ping xmlns='urn:xmpp:ping'/>") "get", "<ping xmlns='urn:xmpp:ping'/>")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Can't send MUC self ping: ", err)
} }
pingSent = time.Now() pingSent = time.Now()
@ -208,7 +208,7 @@ func pingServer(client *xmpp.Client, server string, botJid string) {
// Send ping to server to check if connection still exists. // Send ping to server to check if connection still exists.
err := client.PingC2S(botJid, strings.Split(server, ":")[0]) err := client.PingC2S(botJid, strings.Split(server, ":")[0])
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Client2Server ping failed:", err)
} }
} }
} }
@ -217,7 +217,7 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay. for { // Receive stanzas. ToDo: Receive stanzas continiously without 30s delay.
stanza, err := client.Recv() stanza, err := client.Recv()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed receiving Stanzas:", err)
} }
// Check stanzas, maybe we want to reply. // Check stanzas, maybe we want to reply.
@ -254,7 +254,7 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
"\"feeds\": List feeds I'm following.\n" + "\"feeds\": List feeds I'm following.\n" +
"\"source\": Show source code URL."}) "\"source\": Show source code URL."})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed sending message to MUC:", err)
} }
// Reply with repo address for `source`. // Reply with repo address for `source`.
case "source": case "source":
@ -263,7 +263,7 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
": My source can be found at " + ": My source can be found at " +
"https://salsa.debian.org/mdosch-guest/feed-to-muc"}) "https://salsa.debian.org/mdosch-guest/feed-to-muc"})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed sending message to MUC:", err)
} }
// Reply with the list of monitored feeds for `feeds`. // Reply with the list of monitored feeds for `feeds`.
case "feeds": case "feeds":
@ -277,7 +277,7 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] + Type: "groupchat", Text: strings.Split(v.Remote, "/")[1] +
": Feeds I'm following:\n" + feedList}) ": Feeds I'm following:\n" + feedList})
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed sending message to MUC:", err)
} }
} }
@ -301,13 +301,13 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
"<identity category='client' type='bot' name='feedbot'/>"+ "<identity category='client' type='bot' name='feedbot'/>"+
"<feature var='http://jabber.org/protocol/disco#info'/></query>") "<feature var='http://jabber.org/protocol/disco#info'/></query>")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed to reply to disco#info:", err)
} }
} else if strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>") == true { } else if strings.Contains(string(v.Query), "<ping xmlns='urn:xmpp:ping'/>") == true {
// Reply to pings. // Reply to pings.
_, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "") _, err := client.RawInformation(client.JID(), v.From, v.ID, "result", "")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed to reply to ping:", err)
} }
} else { } else {
// Send error replies for all other IQs. // Send error replies for all other IQs.
@ -315,7 +315,7 @@ func processStanzas(client *xmpp.Client, muc string, mucNick string, feeds []str
"<error type='cancel'><service-unavailable "+ "<error type='cancel'><service-unavailable "+
"xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>") "xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Error: Failed to send error IQ:", err)
} }
} }
} }

View file

@ -38,14 +38,14 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
if _, err := os.Stat(cachePath); os.IsNotExist(err) { if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err = os.MkdirAll(cachePath, 0700) err = os.MkdirAll(cachePath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create cache path:", err)
} }
} }
} else { // Get the current user. } else { // Get the current user.
curUser, err := user.Current() curUser, err := user.Current()
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't get current user:", err)
return "", err return "", err
} }
// Get home directory. // Get home directory.
@ -61,7 +61,7 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
if _, err := os.Stat(cachePath); os.IsNotExist(err) { if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err = os.MkdirAll(cachePath, 0700) err = os.MkdirAll(cachePath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create cache path:", err)
} }
} }
@ -74,7 +74,7 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
if _, err := os.Stat(cachePath); os.IsNotExist(err) { if _, err := os.Stat(cachePath); os.IsNotExist(err) {
err = os.MkdirAll(cachePath, 0700) err = os.MkdirAll(cachePath, 0700)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create hash identifier for cache file:", err)
} }
} }
@ -83,7 +83,7 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
if _, err := os.Stat(cacheFile); os.IsNotExist(err) { if _, err := os.Stat(cacheFile); os.IsNotExist(err) {
file, err = os.Create(cacheFile) file, err = os.Create(cacheFile)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create cache file:", err)
} }
defer file.Close() defer file.Close()
@ -94,26 +94,26 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ") lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
_, err = file.Write(lastUpdateJSON) _, err = file.Write(lastUpdateJSON)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't write last update time stamp to cache file:", err)
} }
} else { } else {
file, err = os.OpenFile(cacheFile, os.O_RDWR, 0600) file, err = os.OpenFile(cacheFile, os.O_RDWR, 0600)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't open cache file:", err)
} }
defer file.Close() defer file.Close()
decoder := json.NewDecoder(file) decoder := json.NewDecoder(file)
lastUpdate := feedCache{} lastUpdate := feedCache{}
if err := decoder.Decode(&lastUpdate); err != nil { if err := decoder.Decode(&lastUpdate); err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't decode laste updates time stamp:", err)
} }
last, err = time.Parse(time.RFC3339, string(lastUpdate.LastChange)) last, err = time.Parse(time.RFC3339, string(lastUpdate.LastChange))
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't parse last updates time stamp:", err)
} }
} }
@ -179,19 +179,19 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
// ToDo: Replace timestamp without deleting. // ToDo: Replace timestamp without deleting.
err = os.Remove(cacheFile) err = os.Remove(cacheFile)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't delete cache file:", err)
} }
file, err = os.Create(cacheFile) file, err = os.Create(cacheFile)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't create cache file:", err)
} }
defer file.Close() defer file.Close()
lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ") lastUpdateJSON, _ := json.MarshalIndent(lastUpdate, "", " ")
_, err = file.Write(lastUpdateJSON) _, err = file.Write(lastUpdateJSON)
if err != nil { if err != nil {
log.Fatal("Error: ", err) log.Fatal("Error: Can't write last update time stamp to cache file:", err)
} }
// Remove redirects and tracking parameters from URL. // Remove redirects and tracking parameters from URL.