github.com/searKing/golang/go@v1.2.117/net/http/interceptors.recover.server.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package http 6 7 import ( 8 "io" 9 "net/http" 10 ) 11 12 // RecoveryServerInterceptor returns a new server interceptors with recovery from panic. 13 // affect as recover{f()}; next() 14 func RecoveryServerInterceptor(next http.Handler, out io.Writer, f func(w http.ResponseWriter, r *http.Request, err any)) http.Handler { 15 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 16 defer func() { 17 Recover(out, r, func(err any) any { 18 if f == nil { 19 return nil 20 } 21 f(w, r, err) 22 return nil 23 }) 24 }() 25 next.ServeHTTP(w, r) 26 }) 27 }