diff --git a/go.mod b/go.mod index 80bae1fb3..89a385060 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/gorilla/sessions v1.2.1 github.com/markbates/grift v1.5.0 github.com/markbates/refresh v1.12.0 - github.com/markbates/safe v1.0.1 github.com/monoculum/formam v3.5.5+incompatible github.com/psanford/memfs v0.0.0-20210214183328-a001468d78ef github.com/sirupsen/logrus v1.8.1 diff --git a/go.sum b/go.sum index 818bb6104..62f5b4eab 100644 --- a/go.sum +++ b/go.sum @@ -341,7 +341,6 @@ github.com/markbates/grift v1.5.0/go.mod h1:1ssFm5gSGmzTkhi3Wfh/nqlU74J73TlAjoDM github.com/markbates/oncer v1.0.0/go.mod h1:Z59JA581E9GP6w96jai+TGqafHPW+cPfRxz2aSZ0mcI= github.com/markbates/refresh v1.12.0 h1:vTgVPX4p77v26auBJhlgaTQ/adWAYSIcVYJvM63Nbpo= github.com/markbates/refresh v1.12.0/go.mod h1:Vpwi1+q+2U1VxE7C0Ilj6r2/+TigRzQcLez6XM3bPLc= -github.com/markbates/safe v1.0.1 h1:yjZkbvRM6IzKj9tlu/zMJLS0n/V351OZWRnF3QfaUxI= github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= diff --git a/plugins.go b/plugins.go index 8c10ac22d..e395c7433 100644 --- a/plugins.go +++ b/plugins.go @@ -11,7 +11,6 @@ import ( "github.com/gobuffalo/buffalo/plugins" "github.com/gobuffalo/envy" "github.com/gobuffalo/events" - "github.com/markbates/safe" ) var loadPlugins sync.Once @@ -35,28 +34,23 @@ func LoadPlugins() error { continue } err := func(c plugins.Command) error { - return safe.RunE(func() error { - n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name) - fn := func(e events.Event) { - b, err := json.Marshal(e) - if err != nil { - fmt.Println("error trying to marshal event", e, err) - return - } - cmd := exec.Command(c.Binary, c.UseCommand, string(b)) - cmd.Stderr = os.Stderr - cmd.Stdout = os.Stdout - cmd.Stdin = os.Stdin - if err := cmd.Run(); err != nil { - fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err) - } - } - _, err := events.NamedListen(n, events.Filter(c.ListenFor, fn)) + n := fmt.Sprintf("[PLUGIN] %s %s", c.Binary, c.Name) + fn := func(e events.Event) { + b, err := json.Marshal(e) if err != nil { - return err + fmt.Println("error trying to marshal event", e, err) + return + } + cmd := exec.Command(c.Binary, c.UseCommand, string(b)) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + if err := cmd.Run(); err != nil { + fmt.Println("error trying to send event", strings.Join(cmd.Args, " "), err) } - return nil - }) + } + _, err := events.NamedListen(n, events.Filter(c.ListenFor, fn)) + return err }(c) if err != nil { errResult = err diff --git a/worker/simple.go b/worker/simple.go index 6097cbb3a..55227342e 100644 --- a/worker/simple.go +++ b/worker/simple.go @@ -2,11 +2,11 @@ package worker import ( "context" + "errors" "fmt" "sync" "time" - "github.com/markbates/safe" "github.com/sirupsen/logrus" ) @@ -122,7 +122,7 @@ func (w *Simple) Perform(job Job) error { w.wg.Add(1) go func() { defer w.wg.Done() - err := safe.RunE(func() error { + err := safeRun(func() error { return h(job.Args) }) @@ -139,6 +139,22 @@ func (w *Simple) Perform(job Job) error { return err } +// safeRun the function safely knowing that if it panics +// the panic will be caught and returned as an error +func safeRun(fn func() error) (err error) { + defer func() { + if ex := recover(); ex != nil { + if e, ok := ex.(error); ok { + err = e + return + } + err = errors.New(fmt.Sprint(ex)) + } + }() + + return fn() +} + // PerformAt performs a job at a particular time using a goroutine. func (w *Simple) PerformAt(job Job, t time.Time) error { return w.PerformIn(job, time.Until(t))