MCPcopy Index your code
hub / github.com/maruel/panicparse / Example_httpHandlerMiddleware

Function Example_httpHandlerMiddleware

stack/example_test.go:190–228  ·  view source on GitHub ↗

Registers a middleware to trap exceptions and report them on http.Handler. For demonstration purposes, start a web server that panics and call into it.

()

Source from the content-addressed store, hash-verified

188// For demonstration purposes, start a web server that panics and call into
189// it.
190func Example_httpHandlerMiddleware() {
191 // Start the web server.
192 ln, err := net.Listen("tcp", "localhost:0")
193 if err != nil {
194 log.Fatal(err)
195 }
196 mux := http.ServeMux{}
197 mux.Handle("/", wrapPanic(http.HandlerFunc(panickingHandler)))
198 ch := make(chan error)
199 go func() {
200 ch <- http.Serve(ln, &mux)
201 }()
202
203 // Call the server once to force a stack trace to be printed.
204 resp, err := http.Get("http://" + ln.Addr().String() + "/")
205 if err != nil {
206 log.Fatal(err)
207 }
208 b, err := io.ReadAll(resp.Body)
209 if err != nil {
210 log.Fatal(err)
211 }
212 resp.Body.Close()
213 if v := string(b); v != "Done" {
214 log.Fatal(v)
215 }
216
217 // Close the server.
218 if err := ln.Close(); err != nil {
219 log.Fatal(err)
220 }
221 <-ch
222 // Output:
223 // recovered: "It happens"
224 // Parsed stack:
225 // stack_test example_test.go:239 recoverPanic(<args>)
226 // stack_test example_test.go:233 panickingHandler(<args>)
227 // stack_test example_test.go:295 Example_httpHandlerMiddleware.wrapPanic.func2(<args>)
228}
229
230// panickingHandler is an http.HandlerFunc that panics.
231func panickingHandler(w http.ResponseWriter, r *http.Request) {

Callers

nothing calls this directly

Calls 2

wrapPanicFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…