Commits

Cédric Bonhomme  committed b7b2f48

Display the error in the webpage if there is a problem while reading the XML file.

  • Participants
  • Parent commits f4c3450

Comments (0)

Files changed (1)

 }
 
 // Check if an error occured
-func checkError(err error) {
+func checkError(err error) int {
     if err != nil {
-        fmt.Println("Fatal error ", err.Error())
-        os.Exit(1)
+        os.Stdout.WriteString("Content-type:text/html\r\n\r\n")
+        os.Stdout.WriteString("<!DOCTYPE html>\n<html>\n<head>\n<title>GoQuotes</title>\n")
+        os.Stdout.WriteString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n")
+        os.Stdout.WriteString("<link rel=\"stylesheet\" type=\"text/css\" href=\"./style.css\" />\n</head>\n")
+        os.Stdout.WriteString("<body>\n")
+        os.Stdout.WriteString("Error " + err.Error())
+        os.Stdout.WriteString("</body>\n</html>")
+        return 1
     }
+    return 0
 }
 
 // Return a random quote.
     var quotes Quotes
 
     err := xml.Unmarshal([]byte(quotes_str), &quotes)
-    checkError(err)
+    if (1 != checkError(err)) {
+        // Get the number of quotes
+        nb_quotes := len(quotes.Quotes)
+        rand.Seed(time.Now().UnixNano())
+        quote := rand.Intn(nb_quotes)
 
-    // Get the number of quotes
-    nb_quotes := len(quotes.Quotes)
-    rand.Seed(time.Now().UnixNano())
-    quote := rand.Intn(nb_quotes)
-
-    // Now use the Quote structure
-    printHTML(quotes.Quotes[quote])
-
+        // Now use the Quote structure
+        printHTML(quotes.Quotes[quote])
+    }
 }
 
 func printHTML(quote Quote) {