Git Product home page Git Product logo

go-panic-catch's Introduction

go-panic-catch

GoDoc Build Status

go-panic-catch is a simple middleware for golang web servers that recovers from panics and enables you to handle panics gracefully.

To get it:

go get github.com/matthewjamesboyle/go-panic-catch

And then to use it:

package main

import (
	"github.com/matthewjamesboyle/go-panic-catch"
	"github.com/matthewjamesboyle/go-panic-catch/catchers"
	"net/http"
)

func main() {
	fn := func(writer http.ResponseWriter, req *http.Request) {
		panic("ut oh")
	}

	//something that satisfies panicHandler interface
	slack := catchers.NewSlack("some-webhook-url")

	server := &http.Server{
		Handler: goCatch.PanicMiddleware(*slack, "recovering from panic will write this to slack", http.HandlerFunc(fn)),
	}

	server.ListenAndServe()
}

It should also work with all the popular web servers such as gorillia mux and gin.

go-panic-catch currently comes with two "catchers" that handle panics.

  • slack catcher - will write a message to slack everytime your web server panics
  • log - Logs "message" every time there is a panic.

Want to implement your own handler? Simply implement the following interface:

type PanicHandler interface {
	HandlePanic(message string) error
}

Some ideas for other handlers:

  • email everytime there is a panic.
  • increment a prometheus counter.

(contributions appreciated if you want to work on these)

go-panic-catch's People

Contributors

matthewjamesboyle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

aartij17 icodein

go-panic-catch's Issues

Code needs license

Hi, saw this from reddit. There is no license included with your code. That will stop people from using it. Apache 2 license is popular in the go community.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.