🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/pivotalsendgrid/gobrake

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/pivotalsendgrid/gobrake

v0.0.0-20131001094741-772c4a45d22a
Source
Go
Version published
Created
Source

Airbrake Golang Notifier

Example:

import (
    "net/http"

    "github.com/airbrake/gobrake"
)

const (
    projectID = 1
    key = "apikey"
    secure = true
)

var Notifier gobrake.Notifier

func init() {
    transport := gobrake.NewJSONTransport(&http.Client{}, projectID, key, secure)
    Notifier = gobrake.NewNotifier(transport)
    Notifier.SetContext("environment", "production")
    Notifier.SetContext("version", "1.0")
}

func handler(w http.ResponseWriter, r *http.Request) {
    defer func() {
        if iface := recover(); iface != nil {
            Notifier.Panic(iface, r, nil)
        }
    }()

    if err := process(r); err != nil {
        go Notifier.Notify(err, r, nil)
    }
}

Appengine

import (
    "net/http"

    "appengine/urlfetch"
    "github.com/airbrake/gobrake"
)

var (
    Notifier *ErrorNotifier
)

func init() {
    transport := gobrake.NewJSONTransport(
        nil, config.AirbrakeProjectID, config.AirbrakeKey, config.AirbrakeSecure)
    Notifier := gobrake.NewNotifier(transport)
    Notifier.SetContext("environment", config.Env)
    Notifier.SetContext("version", config.Version)

    Notifier = &ErrorNotifier{N: Notifier}
    http.Handle("/", &RecoverHandler{H: Router})
}

type ErrorNotifier struct {
    N gobrake.Notifier
}

func (n *ErrorNotifier) Notify(
    c appengine.Context, e error, r *http.Request, session map[string]interface{},
) {
    if v, ok := n.N.Transport().(*gobrake.JSONTransport); ok {
        v.Client = urlfetch.Client(c)
    }
    if err := n.N.Notify(e, r, session); err != nil {
        c.Errorf("Notify failed: %v", err)
    }
}

func (n *ErrorNotifier) Panic(
    c appengine.Context, e interface{}, r *http.Request, session map[string]interface{},
) {
    if v, ok := n.N.Transport().(*gobrake.JSONTransport); ok {
        v.Client = urlfetch.Client(c)
    }
    if err := n.N.Panic(e, r, session); err != nil {
        c.Errorf("Notify failed: %v", err)
    }
}

type RecoverHandler struct {
    H http.Handler
}

func (h *RecoverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    defer func() {
        if iface := recover(); iface != nil {
            c := appengine.NewContext(r)
            Notifier.Panic(c, iface, r, nil)
            panic(iface)
        }
    }()
    h.H.ServeHTTP(w, r)
}

FAQs

Package last updated on 01 Oct 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts