add recovery handler

This commit is contained in:
David 2020-09-12 17:13:58 -04:00
parent cb3282304b
commit f3af5c9020

24
main.go
View File

@ -91,6 +91,13 @@ type geminiHandler interface {
Handle(geminiResponse, geminiRequest)
}
type geminiHandlerFunc func(geminiResponse, geminiRequest)
// Handle calls f(w, r).
func (f geminiHandlerFunc) Handle(w geminiResponse, r geminiRequest) {
f(w, r)
}
// implementations
type staticGeminiHandler struct {
StaticString string
@ -163,6 +170,21 @@ func (h fsGeminiHandler) Handle(w geminiResponse, r geminiRequest) {
io.Copy(w, source)
}
func recoveryHandler(next geminiHandler) geminiHandler {
return geminiHandlerFunc(func(w geminiResponse, r geminiRequest) {
defer func() {
err := recover()
if err != nil {
log.Println(err)
w.WriteStatus(responseCodes["TEMPORARYFAILURE"], "Internal Error")
return
}
}()
next.Handle(w, r)
})
}
// handler for general http queries (fallthrough for certmagic)
type genericHTTPHandler struct {
StaticString string
@ -192,7 +214,7 @@ func handleConnection(log Logger, conn net.Conn, h geminiHandler) {
r := request{
url: u,
}
h.Handle(&w, r)
recoveryHandler(h).Handle(&w, r)
}
func main() {