1
0
mirror of https://git.sr.ht/~yotam/shavit synced 2024-11-23 00:42:10 +01:00

Recover from panics in the main handler

This commit is contained in:
Yotam Nachum 2019-12-06 21:15:28 +02:00
parent b612902e77
commit bbc61ff2a9
2 changed files with 21 additions and 1 deletions

@ -21,7 +21,7 @@ func main() {
log.Fatal(err)
}
handler := LoggingHandler{Handler{cfg}}
handler := LoggingHandler{RecovererHandler{Handler{cfg}}}
err = gemini.ListenAndServe("", cfg.TLSCert, cfg.TLSKey, handler)
if err != nil {
log.Fatal(err)

20
recover.go Normal file

@ -0,0 +1,20 @@
package main
import (
gemini "git.sr.ht/~yotam/go-gemini"
)
// RecovererHandler wrap a Gemini handler and recover from panics
type RecovererHandler struct {
handler gemini.Handler
}
// Handle implement the gemini.Handler interface by recovering inner handler
func (h RecovererHandler) Handle(req gemini.Request) (res gemini.Response) {
defer func() {
if r := recover(); r != nil {
res = gemini.Response{Status: gemini.StatusTemporaryFailure, Meta: "Internal server error", Body: nil}
}
}()
return h.handler.Handle(req)
}