Update server/main.go to show GopherJS errors

This commit is contained in:
Hajime Hoshi 2015-01-06 22:24:34 +09:00
parent c98631a703
commit b769e0eb6c

View File

@ -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="<pre style='white-space: pre-wrap;'><code>` + t + `</code></pre>";
}`
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
}
}