From b769e0eb6c81796dd50262749f19607dc23be43a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 6 Jan 2015 22:24:34 +0900 Subject: [PATCH] Update server/main.go to show GopherJS errors --- example/server/main.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/example/server/main.go b/example/server/main.go index fe5d16ef6..31ad16aff 100644 --- a/example/server/main.go +++ b/example/server/main.go @@ -15,8 +15,10 @@ package main import ( + "errors" "flag" "fmt" + "html/template" "io" "io/ioutil" "log" @@ -61,8 +63,10 @@ func createJSIfNeeded(name string) (string, error) { } if (err != nil && os.IsNotExist(err)) || time.Now().Sub(stat.ModTime()) > 10*time.Second { target := "github.com/hajimehoshi/ebiten/example/" + name - if err := exec.Command("gopherjs", "build", "-o", out, target).Run(); err != nil { - return "", err + out, err := exec.Command("gopherjs", "build", "-o", out, target).CombinedOutput() + if err != nil { + log.Print(string(out)) + return "", errors.New(string(out)) } } return out, nil @@ -97,19 +101,22 @@ func appName(r *http.Request) (string, error) { func serveMainJS(w http.ResponseWriter, r *http.Request) { name, err := appName(r) if err != nil { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprint(w, err.Error()) + http.Error(w, err.Error(), http.StatusBadRequest) return } out, err := createJSIfNeeded(name) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprint(w, err.Error()) + t := template.JSEscapeString(template.HTMLEscapeString(err.Error())) + js := ` +window.onload = function() { + document.body.innerHTML="
` + t + `
"; +}` + w.Header().Set("Content-Type", "text/javascript") + fmt.Fprintf(w, js) return } if err := serveFile(w, out, "text/javascript"); err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprint(w, err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) return } } @@ -117,20 +124,17 @@ func serveMainJS(w http.ResponseWriter, r *http.Request) { func serveMainJSMap(w http.ResponseWriter, r *http.Request) { name, err := appName(r) if err != nil { - w.WriteHeader(http.StatusBadRequest) - fmt.Fprint(w, err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) return } out, err := createJSIfNeeded(name) if err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprint(w, err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) return } out += ".map" if err := serveFile(w, out, "application/octet-stream"); err != nil { - w.WriteHeader(http.StatusInternalServerError) - fmt.Fprint(w, err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) return } }