Fix up error handling

This commit is contained in:
Eliot Whalan 2016-06-11 13:33:29 +10:00
parent a8b31ce373
commit c28006cc84
1 changed files with 14 additions and 6 deletions

20
main.go
View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"github.com/dchest/uniuri" "github.com/dchest/uniuri"
"github.com/ewhal/pygments" "github.com/ewhal/pygments"
"io" "io"
@ -17,9 +18,9 @@ const (
PORT = ":8080" PORT = ":8080"
) )
func check(e error) { func check(err error) {
if e != nil { if err != nil {
panic(e) fmt.Println(err)
} }
} }
@ -63,11 +64,14 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
if param1 != "" { if param1 != "" {
d := DIRECTORY + param1 d := DIRECTORY + param1
s, err := ioutil.ReadFile(d) s, err := ioutil.ReadFile(d)
check(err) if err != nil {
http.Error(w, err.Error(), 500)
}
if param2 != "" { if param2 != "" {
highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8") highlight := pygments.Highlight(string(s), param2, "html", "full, style=autumn,linenos=True, lineanchors=True,anchorlinenos=True,", "utf-8")
io.WriteString(w, string(highlight)) io.WriteString(w, string(highlight))
} else { } else {
io.WriteString(w, string(s)) io.WriteString(w, string(s))
} }
@ -76,7 +80,9 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
} }
case "POST": case "POST":
buf, err := ioutil.ReadAll(r.Body) buf, err := ioutil.ReadAll(r.Body)
check(err) if err != nil {
http.Error(w, err.Error(), 500)
}
io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n") io.WriteString(w, ADDRESS+"?p="+save(buf)+"\n")
case "DELETE": case "DELETE":
// Remove the record. // Remove the record.
@ -86,6 +92,8 @@ func pasteHandler(w http.ResponseWriter, r *http.Request) {
func main() { func main() {
http.HandleFunc("/", pasteHandler) http.HandleFunc("/", pasteHandler)
err := http.ListenAndServe(PORT, nil) err := http.ListenAndServe(PORT, nil)
check(err) if err != nil {
panic(err)
}
} }