Simple package to catch & notify your panic or exceptions via slack or save into files.
import "github.com/tokopedia/panics"
panics.SetOptions(&panics.Options{
Env: "TEST",
SlackWebhookURL: "https://hooks.slack.com/services/blablabla/blablabla/blabla",
Filepath: "/var/log/myapplication", // it'll generate panics.log
Channel: "slackchannel",
Tags: panics.Tags{"host": "127.0.0.1", "datacenter":"aws"},
})
panics.Capture(
"Deposit Anomaly",
`{"user_id":123, "deposit_amount" : -100000000}`,
)
http.HandleFunc("/", panics.CaptureHandler(func(w http.ResponseWriter, r *http.Request) {
panic("Duh aku panik nih guys")
}))
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
router := httprouter.New()
router.POST("/", panics.CaptureHTTPRouterHandler(func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
panic("Duh httprouter aku panik nih guys")
}))
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
panic("Duh aku panik nih guys")
})
negro := negroni.New()
negro.Use(negroni.HandlerFunc(CaptureNegroniHandler))
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
q, _ := nsq.NewConsumer("topic", "channel", nsq.NewConfig())
q.AddHandler(panics.CaptureNSQConsumer(func(message *nsq.Message) error {
var x *int
fmt.Println(*x)
message.Finish()
return nil
}))
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
//for example we use chi router
r := chi.NewRouter()
r.Use(panics.HTTPRecoveryMiddleware)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
panic("panic from affiliate server")
})
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
server := grpc.NewServer(
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
// use another interceptor
grpc_validator.UnaryServerInterceptor(),
// use panic's unary interceptor
panics.UnaryServerInterceptor,
)),
)
Panic inside goroutine in the call stack will not be handled, use CaptureGoroutine
// doSomething will be called with a goroutine
func doSomething(dataChan chan string) {
panic("duh aku ingin panik di dalam goroutine guys")
dataChan <- "MyData"
}
func MyHTTPHandler() {
dataChan := make(chan string)
go panics.CaptureGoroutine(
func(){
// function to be executed inside goroutine
doSomething(dataChan)
},
func(){
// recovery codes in-case panics happen
//in this case, fill channel with empty data
dataChan <- ""
}
)
select {
case data := <- dataChan:
fmt.Println(data)
default:
}
}