gitlab.com/gitlab-org/labkit@v1.21.0/errortracking/examples_test.go (about)

     1  package errortracking_test
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"gitlab.com/gitlab-org/labkit/errortracking"
     7  )
     8  
     9  func ExampleCapture() {
    10  	req, err := http.NewRequest("GET", "http://example.com", nil)
    11  	ctx := req.Context()
    12  
    13  	if err != nil {
    14  		// Send the error to the error tracking system
    15  		errortracking.Capture(err,
    16  			errortracking.WithContext(ctx),                          // Extract details such as correlation-id from context
    17  			errortracking.WithRequest(req),                          // Extract additional details from request
    18  			errortracking.WithField("domain", "http://example.com"), // Add additional custom details
    19  			errortracking.WithStackTrace(),                          // Attach the stack trace of up to 10 errors in the chain
    20  		)
    21  	}
    22  }
    23  
    24  func ExampleNewHandler() {
    25  	handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
    26  		panic("oh dear")
    27  	})
    28  
    29  	http.ListenAndServe(":1234", errortracking.NewHandler(handler))
    30  }