Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with go-chi #106

Open
josemarluedke opened this issue Mar 17, 2019 · 7 comments
Open

Example with go-chi #106

josemarluedke opened this issue Mar 17, 2019 · 7 comments

Comments

@josemarluedke
Copy link

Can we have a example to setup bugsnag with go-chi?

@josemarluedke
Copy link
Author

Anyone?

@bengourley
Copy link
Contributor

Hi @josemarluedke, thanks for requesting this. I've made a note internally to confirm support for chi and if so to put together an example project. It could be some time until we get around to it as we have lots of higher priority things to do at the moment. Thanks for your patience!

@josemarluedke
Copy link
Author

Any news on this?

@snmaynard
Copy link
Contributor

I think it's unlikely we will get to it any time in the near future as we are working on other things at the moment. Sorry I can't be more helpful right now!

@jmonteiro
Copy link

jmonteiro commented Jan 5, 2022

As go-chi is just a router, you can very easily get it up and running with yourrouter.Use(bugsnag.Handler) like in the example:

package main

import (
	"net/http"

	"github.com/bugsnag/bugsnag-go/v2"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	r := chi.NewRouter()
	r.Use(bugsnag.Handler) // <- here, make sure it's the first one on the chain
	r.Use(middleware.RequestID)
	r.Use(middleware.Logger)

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})

	http.ListenAndServe(":3333", r)
}

Note that you can't use chi's provided middleware.Recoverer as it will bury errors and prevent Bugsnag from capturing them.

@jpoz
Copy link

jpoz commented Aug 24, 2023

Edit

Default middleware only captures panics, but not errors that are returned to the user. Wrote a quick middlware, that captures panics and http errors >404:

note: since the error is created in the middleware itself, the stacktrace will always be the same. I plan to attach errors to the context and get them out in the defer statement.

package server

import (
	"fmt"
	"net/http"

	"github.com/bugsnag/bugsnag-go/v2"
	"github.com/go-chi/chi/v5/middleware"
)

func ErrorHandler(next http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		request := r
		ctx := bugsnag.StartSession(r.Context())
		defer bugsnag.AutoNotify(ctx) // capture panics
		ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)

		defer func() {
			status := ww.Status()
			if status > 404 { // capture http errors
				bugsnag.Notify(fmt.Errorf("%s %s returned %d", r.Method, r.URL, status), request)
			}
		}()

		next.ServeHTTP(ww, r.WithContext(ctx))
	}
	return http.HandlerFunc(fn)
}

Just panics:

I needed to wrap the handler (it was taking an extra variable). This worked for me:

package main

import (
	"net/http"

	"github.com/bugsnag/bugsnag-go/v2"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	r := chi.NewRouter()
	r.Use(func(next http.Handler) http.Handler { return bugsnag.Handler(next) }) // <- here, make sure it's the first one on the chain
	r.Use(middleware.RequestID)
	r.Use(middleware.Logger)

	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})

	http.ListenAndServe(":3333", r)
}

@dilettantemode
Copy link

Thanks, it seems just wrapping a recoverer after bugsnag works well, here is my setup incase someone need a quick fix

package main

import (
	"net/http"
	bugsnag "github.com/bugsnag/bugsnag-go/v2"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	router := chi.NewRouter()
	router.Use(ErrorHandler)

	// Your routes here

	http.ListenAndServe(":8080", r)
}

func ErrorHandler(next http.Handler) http.Handler {
	return middleware.Recoverer(bugsnag.Handler(next))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants