Permalink
Browse files

Better error reporting

* format errors through br instead of \n
* make them easy to read
* include context of when they happened
  • Loading branch information...
1 parent 40a64c3 commit 1d88d4f4b070094996ea30011ebd872d97dd9d32 @tobi committed Dec 30, 2011
Showing with 35 additions and 9 deletions.
  1. +31 −5 error.go
  2. +4 −4 main.go
View
@@ -4,18 +4,44 @@ import (
"template"
"log"
"io"
+ "regexp"
)
var tmpl *template.Template
+var ex *regexp.Regexp
+
+func n2br(text string) string {
+ log.Print("Got text: " + text)
+ return ex.ReplaceAllString(text, "<br>")
+}
+
+var fmap = template.FuncMap{
+ "n2br": n2br,
+}
func init() {
- tmpl = template.Must(template.New("error").Parse("\n\n<!DOCTYPE html><html><head><title>Error</title></head><body style='font-style:sans-sarief;font-size:11pt;background:#eee'><div style='border:1px solid #999;background:white;margin: 50px auto;padding:1em 3em;width:400px'>{{. | html}}</div></body></html>\n" ))
+ layout := template.New("error template")
+ layout.Funcs(fmap)
+ tmpl = template.Must(layout.Parse(`
+ <!DOCTYPE html>
+ <html>
+ <head><title>Error: {{.title | html}}</title></head>
+ <body style='font-style:sans-serif;font-size:10pt;background:#eee'>
+ <div style='border:1px solid #999;background:white;margin: 50px auto;padding:1em 3em;width:600px'>
+ <h2>{{.title | html | n2br}}</h2>
+ <pre style='background:#222;color:#eee;padding:8px 5px;border:1px solid #666'>{{.error | html | n2br}}</pre>
+ </div>
+ </body>
+ </html>`))
+
+ ex = regexp.MustCompile("\n")
}
-func error(client io.WriteCloser, s string) {
- log.Printf("Error: %s", s)
+func error(client io.WriteCloser, title, error string) {
+ log.Printf("---\nError in %s: \n\n%s\n\n", title, error)
client.Write([]byte("HTTP/1.0 500 Internal Error\n"))
- client.Write([]byte("Content-Type: text/html\n"))
- tmpl.Execute(client, s)
+ client.Write([]byte("Content-Type: text/html\n\n"))
+ tmpl.Execute(client, map[string]string{"error": error, "title": title})
+ client.Write([]byte("\n"))
client.Close()
}
View
@@ -59,7 +59,7 @@ func forward(client net.Conn) {
output, err := cmd.CombinedOutput()
if err != nil {
- error(client, string(output))
+ error(client, "Compiling", string(output))
return
}
@@ -69,7 +69,7 @@ func forward(client net.Conn) {
err = cmd.Start()
if err != nil {
- error(client, err.String())
+ error(client, "Starting child process", err.String())
return
}
@@ -81,7 +81,7 @@ func forward(client net.Conn) {
if err == nil {
if pipeRequestResponse(server, client) != nil {
- error(client, err.String())
+ error(client, "Forwarding inbound request to child", err.String())
}
return
@@ -90,7 +90,7 @@ func forward(client net.Conn) {
time.Sleep(2e8) // 200ms
}
- error(client, fmt.Sprintf("Timeout waiting to connect to port %d. Perhaps the executable doesn't support the -p parameter?", client_port))
+ error(client, "Connecting to child process", fmt.Sprintf("Timeout waiting to connect to port %d. Perhaps the executable doesn't support the -p parameter?", client_port))
}
func main() {

0 comments on commit 1d88d4f

Please sign in to comment.