Add error handling to all functions that can possibly return non-nil errors
This commit is contained in:
parent
09bff1ed18
commit
56c6565cb1
|
@ -27,7 +27,7 @@ const (
|
|||
<meta name="theme-color" content="#ffffff">`
|
||||
)
|
||||
|
||||
func serveFavicons() httprouter.Handle {
|
||||
func serveFavicons(errorChannel chan<- error) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
fname := strings.TrimPrefix(r.URL.Path, "/")
|
||||
|
||||
|
@ -36,8 +36,18 @@ func serveFavicons() httprouter.Handle {
|
|||
return
|
||||
}
|
||||
|
||||
w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
|
||||
err = w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
17
cmd/index.go
17
cmd/index.go
|
@ -262,13 +262,28 @@ func (index *fileIndex) Import(path string) error {
|
|||
|
||||
func serveIndexRebuild(args []string, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
startTime := time.Now()
|
||||
|
||||
index.clear()
|
||||
|
||||
fileList(args, &filters{}, "", index, formats, errorChannel)
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
|
||||
w.Write([]byte("Ok\n"))
|
||||
_, err := w.Write([]byte("Ok\n"))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Index rebuild requested by %s took %s\n",
|
||||
startTime.Format(logDate),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
|
|||
|
||||
htmlBody.WriteString(`</table></body></html>`)
|
||||
|
||||
length, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
|
||||
written, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -153,7 +153,7 @@ func serveIndexHtml(args []string, index *fileIndex, shouldPaginate bool) httpro
|
|||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: HTML index page (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(length),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
|
|
|
@ -17,7 +17,7 @@ import (
|
|||
|
||||
const (
|
||||
AllowedCharacters string = `^[A-z0-9.\-_]+$`
|
||||
ReleaseVersion string = "4.0.3"
|
||||
ReleaseVersion string = "4.0.4"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
44
cmd/web.go
44
cmd/web.go
|
@ -253,6 +253,8 @@ func serveRoot(paths []string, regexes *regexes, index *fileIndex, formats types
|
|||
|
||||
func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats types.Types, errorChannel chan<- error) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
startTime := time.Now()
|
||||
|
||||
filters := &filters{
|
||||
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
|
||||
excluded: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
|
||||
|
@ -384,8 +386,6 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
|
|||
|
||||
htmlBody.WriteString(`</body></html>`)
|
||||
|
||||
startTime := time.Now()
|
||||
|
||||
formattedPage := gohtml.Format(htmlBody.String())
|
||||
|
||||
written, err := io.WriteString(w, formattedPage)
|
||||
|
@ -409,19 +409,45 @@ func serveMedia(paths []string, regexes *regexes, index *fileIndex, formats type
|
|||
}
|
||||
|
||||
if Russian {
|
||||
kill(path, index)
|
||||
err := kill(path, index)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func serveVersion() httprouter.Handle {
|
||||
func serveVersion(errorChannel chan<- error) httprouter.Handle {
|
||||
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||
startTime := time.Now()
|
||||
|
||||
data := []byte(fmt.Sprintf("roulette v%s\n", ReleaseVersion))
|
||||
|
||||
w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
|
||||
err := w.Header().Write(bytes.NewBufferString("Content-Length: " + strconv.Itoa(len(data))))
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
w.Write(data)
|
||||
return
|
||||
}
|
||||
|
||||
written, err := w.Write(data)
|
||||
if err != nil {
|
||||
errorChannel <- err
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if Verbose {
|
||||
fmt.Printf("%s | SERVE: Version page (%s) to %s in %s\n",
|
||||
startTime.Format(logDate),
|
||||
humanReadableSize(written),
|
||||
realIP(r),
|
||||
time.Since(startTime).Round(time.Microsecond),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -578,15 +604,15 @@ func ServePage(args []string) error {
|
|||
registerHandler(mux, "/", redirectRoot())
|
||||
}
|
||||
|
||||
registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons())
|
||||
registerHandler(mux, Prefix+"/favicons/*favicon", serveFavicons(errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+"/favicon.ico", serveFavicons())
|
||||
registerHandler(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, index, formats, errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, index, errorChannel))
|
||||
|
||||
registerHandler(mux, Prefix+"/version", serveVersion())
|
||||
registerHandler(mux, Prefix+"/version", serveVersion(errorChannel))
|
||||
|
||||
if Index {
|
||||
registerHandler(mux, Prefix+AdminPrefix+"/index/rebuild", serveIndexRebuild(args, index, formats, errorChannel))
|
||||
|
|
Loading…
Reference in New Issue