]> Cypherpunks repositories - gostls13.git/commitdiff
doc: add error handling on http.ListenAndServe
authorFrancesc Campoy Flores <campoy@golang.org>
Thu, 3 Aug 2017 17:52:42 +0000 (10:52 -0700)
committerFrancesc Campoy Flores <campoy@golang.org>
Thu, 31 Aug 2017 17:52:01 +0000 (17:52 +0000)
Fixes #19511

Change-Id: I5585726773b822dba0be0196961132323ebbe084
Reviewed-on: https://go-review.googlesource.com/53071
Reviewed-by: Chris Broadfoot <cbro@golang.org>
12 files changed:
doc/articles/wiki/final-noclosure.go
doc/articles/wiki/final-noerror.go
doc/articles/wiki/final-parsetemplate.go
doc/articles/wiki/final-template.go
doc/articles/wiki/final-test.patch
doc/articles/wiki/final.go
doc/articles/wiki/http-sample.go
doc/articles/wiki/index.html
doc/articles/wiki/notemplate.go
doc/articles/wiki/part2.go
doc/articles/wiki/part3-errorhandling.go
doc/articles/wiki/part3.go

index d72ca805b8793a2603e406e239dfb6289b5cde74..b4ce2557426a8376d6af733ffea8ae74b1d1ad32 100644 (file)
@@ -8,6 +8,7 @@ import (
        "errors"
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
        "regexp"
 )
@@ -98,5 +99,5 @@ func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
        http.HandleFunc("/save/", saveHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index 86d8da751f9d5d77e4c602266568f73558b8763a..42a22da9dd82c35fd95fd175e6723aa4861da78e 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -49,5 +50,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
 func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index 5ff8bf60c5b02f9a1abc62973a04ef2c06a9cd1c..a9aa7f289437d6ab978b79e53bceec54e3b6dab3 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
        "regexp"
 )
@@ -87,5 +88,5 @@ func main() {
        http.HandleFunc("/view/", makeHandler(viewHandler))
        http.HandleFunc("/edit/", makeHandler(editHandler))
        http.HandleFunc("/save/", makeHandler(saveHandler))
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index 719157da954bd23b4d4048164ae18694284f9b96..7ea480e50a94ca6cb61657ffca438a3d53440302 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -61,5 +62,5 @@ func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
        http.HandleFunc("/save/", saveHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index 499ad789b311798e5dba422dea552f3bfe6d56ec..510825b319c4fe412611f09a6952a70128f768c5 100644 (file)
@@ -16,7 +16,7 @@
        http.HandleFunc("/edit/", makeHandler(editHandler))
        http.HandleFunc("/save/", makeHandler(saveHandler))
   
-!      http.ListenAndServe(":8080", nil)
+!      log.Fatal(http.ListenAndServe(":8080", nil))
   }
 --- 87,101 ----
        http.HandleFunc("/edit/", makeHandler(editHandler))
index 139a323010830e7100b86002bcd866fb15f3f752..0f6646ba879aae6c2516a5ef8da2aa6f24cbd0cf 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
        "regexp"
 )
@@ -85,5 +86,5 @@ func main() {
        http.HandleFunc("/edit/", makeHandler(editHandler))
        http.HandleFunc("/save/", makeHandler(saveHandler))
 
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index ac8cc4f2d6739848fd2babaa50dbfb31c151a863..9bc2084c67211b8d2d3fe263e1793e915d60f571 100644 (file)
@@ -2,6 +2,7 @@ package main
 
 import (
        "fmt"
+       "log"
        "net/http"
 )
 
@@ -11,5 +12,5 @@ func handler(w http.ResponseWriter, r *http.Request) {
 
 func main() {
        http.HandleFunc("/", handler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index b6b080df960362a6f7438b10c223810da0c9747c..e5054f7bf74f7189e5c37f3e449057fea1c6a160 100644 (file)
@@ -213,6 +213,12 @@ worry about its second parameter, <code>nil</code>, for now.)
 This function will block until the program is terminated.
 </p>
 
+<p>
+<code>ListenAndServe</code> always returns an error, since it only returns when an
+unexpected error occurs.
+In order to log that error we wrap the function call with <code>log.Fatal</code>.
+</p>
+
 <p>
 The function <code>handler</code> is of the type <code>http.HandlerFunc</code>.
 It takes an <code>http.ResponseWriter</code> and an <code>http.Request</code> as
index be214d1111d6b6ffed44229f7799c39f2d47601e..0fda7a98ce5d29e692212e32fa91a01f2478907c 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "fmt"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -52,5 +53,5 @@ func editHandler(w http.ResponseWriter, r *http.Request) {
 func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index c0231693efbd29373a1a05c2bedd2b2812eadb04..30f9dcf146dc09eab2a811f5207e291868332a98 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "fmt"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -37,5 +38,5 @@ func viewHandler(w http.ResponseWriter, r *http.Request) {
 
 func main() {
        http.HandleFunc("/view/", viewHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index bb4ecda84bb90d3015a97d70f333587a77160454..34b13a6086495d44aa3b533ce78311760267a450 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -69,5 +70,5 @@ func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
        http.HandleFunc("/save/", saveHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }
index 174f3abcd76fa94c7979a7c63aca2e1729660d61..5e5d5056c493d03c7d6c4a1235370ca6070f81d6 100644 (file)
@@ -7,6 +7,7 @@ package main
 import (
        "html/template"
        "io/ioutil"
+       "log"
        "net/http"
 )
 
@@ -53,5 +54,5 @@ func main() {
        http.HandleFunc("/view/", viewHandler)
        http.HandleFunc("/edit/", editHandler)
        //http.HandleFunc("/save/", saveHandler)
-       http.ListenAndServe(":8080", nil)
+       log.Fatal(http.ListenAndServe(":8080", nil))
 }