Skip to content

Commit 07e1806

Browse files
committed
add example for CustomPanicHandler
1 parent 771c9ff commit 07e1806

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

examples/customPanicHandler/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/xujiajun/gorouter"
6+
"log"
7+
"net/http"
8+
)
9+
10+
func main() {
11+
mux := gorouter.New()
12+
mux.PanicHandler = func(w http.ResponseWriter, req *http.Request, err interface{}) {
13+
w.WriteHeader(http.StatusInternalServerError)
14+
fmt.Println("err from recover is :", err)
15+
fmt.Fprint(w, "received a panic")
16+
}
17+
mux.GET("/panic", func(w http.ResponseWriter, r *http.Request) {
18+
panic("panic")
19+
})
20+
21+
log.Fatal(http.ListenAndServe(":8181", mux))
22+
}

0 commit comments

Comments
 (0)