Skip to content
forked from alileza/panics

Simple package to catch & notify your panic or exceptions via slack or save into files.

Notifications You must be signed in to change notification settings

tokopedia/panics

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Panics GoDoc CircleCI GoReportCard

Simple package to catch & notify your panic or exceptions via slack or save into files.

import "github.com/tokopedia/panics"

Configuration

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"},
})

Capture Custom Error

panics.Capture(
    "Deposit Anomaly",
    `{"user_id":123, "deposit_amount" : -100000000}`,
)

Capture Panic on HTTP Handler

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

Capture Panic on httprouter handler

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

Capture Panic on negroni custom middleware

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

Capture panic on nsq consumer

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

Use panic as middleware on standard http.Handler

//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

Use panic as server interceptor on GRPC Server

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

Use panic to wrap goroutine call

// 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:
	}
}

Example

Slack Notification

Notification Example

Authors

About

Simple package to catch & notify your panic or exceptions via slack or save into files.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%