Vincent Schüßler avatar Vincent Schüßler committed dd8542a

Use panic() and recover() for errors

Comments (0)

Files changed (3)

 
 const serveProtocol = HTTP
 const lnet = "tcp"
-const laddr = "localhost:8080"
+const laddr = ":8080"
 
 var appViews = make(map[string]View)
 var postRegex = regexp.MustCompile("/" + postDir + "/[\\d]+-[\\w-]+")
 
 func (app *App) Run() {
 	listener, err := net.Listen(lnet, laddr)
-	fatalError(err)
+	if err != nil { panic(err) }
 
 	switch serveProtocol {
 	case HTTP:
 	}
 
 	app, err := NewApp(*flagEnv)
-	fatalError(err)
+	if err != nil { panic(err) }
 
 	app.Run()
 }
 
-func fatalError(err error) {
-	if err != nil {
-		logError(err)
-		os.Exit(1)
-	}
-}
-
 func logError(err error) {
 	fmt.Fprintln(os.Stderr, err)
 }
 	"strings"
 )
 
-func handle(w http.ResponseWriter, r *http.Request) {
-}
-
 func handleNotFound(w http.ResponseWriter, r *http.Request) {
 	fmt.Fprintln(w, "Not Found: "+r.URL.Path)
 	fmt.Fprintln(os.Stderr, "Not Found: "+r.URL.Path)
 
 func handlePost(app *App, w http.ResponseWriter, r *http.Request) {
 	post, err := getPost(r.URL.Path)
-	if err != nil {
-		fmt.Fprint(os.Stderr, err)
-		handleNotFound(w, r)
-		return
-	}
+	if err != nil { panic(err) }
 	postBody, _ := app.Theme.RenderTemplate("post_full.html", map[string]string{
 		"post_title": post.Title,
 		"content":    string(post.Content),
 	var posts bytes.Buffer
 	for _, p := range getPostList() {
 		post, err := getPostByName(p)
-		if err != nil {
-			logError(err)
-			continue
-		}
+		if err != nil { panic(err) }
 		postBody, _ := app.Theme.RenderTemplate("post_small.html", map[string]string{
 			"post_title": post.Title,
 			"content":    string(post.Content),
 }
 
 func viewRoot(app *App, w http.ResponseWriter, r *http.Request) {
+	defer func() {
+		if err := recover(); err != nil {
+			// TODO
+			logError(err.(error))
+			handleNotFound(w, r)
+		}
+	}()
 	if r.URL.Path == "/" {
 		handlePostList(app, w, r)
 	} else if r.URL.Path == "/feed" {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.