forked from mikey179/golang-error-lightning-talk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userhandler.go
43 lines (42 loc) · 1.35 KB
/
userhandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import (
"fmt"
"net/http"
)
// START OMIT
func deleteStream(s Streams, u Upstream) myHttpHandler {
return func(w *responseWriter, r *http.Request) (reterr error) {
data := map[string]interface{}
stream, err := s.FindByPath(r.URL.Path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
data["title"] = "Internal Server Error"
data["error"] = "stream_lookup_error"
reterr = fmt.Errorf("could not load stream %s from database: %v", r.URL.Path, err)
} else if stream == nil {
w.WriteHeader(http.StatusNotFound)
data["title"] = "Not Found"
data["error"] = "stream_does_not_exist"
} else {
// END OMIT
data["title"] = stream.Title
nowStreaming, err := u.NowStreaming(stream.Path)
if err != nil {
w.WriteHeader(http.StatusBadGateway)
data["error"] = "stream_availability_error_not_sure_if_safe_to_delete"
reterr = fmt.Errorf("failed to check availability for stream %q: %v", stream.Path, err)
} else if nowStreaming {
w.WriteHeader(http.StatusLocked)
data["error"] = "stream_can_not_be_deleted_while_streaming"
} else {
err := s.Delete(stream)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
data["error"] = "could_not_delete_stream"
reterr = fmt.Errorf("failed to delete stream %q: %v", stream.Path, err)
}
}
}
w.Render("deleted.gtmpl", data)
return
}
}