github.com/searKing/golang/go@v1.2.117/net/http/interceptors.recover.client.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 // RecoveryClientInterceptor returns a new client interceptors with recovery from panic. 13 // affect as recover{f()}; next() 14 func RecoveryClientInterceptor(next http.RoundTripper, out io.Writer, f func(resp *http.Response, req *http.Request, err any)) http.RoundTripper { 15 return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) { 16 defer func() { 17 Recover(out, req, func(err any) any { 18 if f == nil { 19 return nil 20 } 21 f(resp, req, err) 22 return nil 23 }) 24 }() 25 resp, err = next.RoundTrip(req) 26 return 27 }) 28 }