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

Add stack trace when panics occur #215

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package work

// Credits: https://github.com/honeybadger-io/honeybadger-go/blob/master/error.go

import (
"fmt"
"reflect"
"runtime"
"strconv"
)

const maxFrames = 20

// Frame represent a stack frame inside of a Honeybadger backtrace.
type Frame struct {
Number string `json:"number"`
File string `json:"file"`
Method string `json:"method"`
}

// Error provides more structured information about a Go error.
type Error struct {
err interface{}
Message string
Class string
Stack []*Frame
}

func (e Error) Error() string {
return e.Message
}

func newError(thing interface{}, stackOffset int) Error {
var err error

switch t := thing.(type) {
case Error:
return t
case error:
err = t
default:
err = fmt.Errorf("%v", t)
}

return Error{
err: err,
Message: err.Error(),
Class: reflect.TypeOf(err).String(),
Stack: generateStack(stackOffset),
}
}

func generateStack(offset int) []*Frame {
stack := make([]uintptr, maxFrames)
length := runtime.Callers(2+offset, stack[:])

frames := runtime.CallersFrames(stack[:length])
result := make([]*Frame, 0, length)

for {
frame, more := frames.Next()

result = append(result, &Frame{
File: frame.File,
Number: strconv.Itoa(frame.Line),
Method: frame.Function,
})

if !more {
break
}
}

return result
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ require (
github.com/garyburd/redigo v1.6.0 // indirect
github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b
github.com/gocraft/work v0.5.1
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/jrallison/go-workers v0.0.0-20180112190529-dbf81d0b75bb
github.com/kr/pretty v0.2.0 // indirect
github.com/orfjackal/nanospec.go v0.0.0-20120727230329-de4694c1d701 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.5.1
github.com/youtube/vitess v2.1.1+incompatible // indirect
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0 h1:pKjeDsx7HGGbjr7V
github.com/gocraft/health v0.0.0-20170925182251-8675af27fef0/go.mod h1:rWibcVfwbUxi/QXW84U7vNTcIcZFd6miwbt8ritxh/Y=
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b h1:g2Qcs0B+vOQE1L3a7WQ/JUUSzJnHbTz14qkJSqEWcF4=
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b/go.mod h1:Ag7UMbZNGrnHwaXPJOUKJIVgx4QOWMOWZngrvsN6qak=
github.com/gocraft/work v0.5.1 h1:3bRjMiOo6N4zcRgZWV3Y7uX7R22SF+A9bPTk4xRXr34=
github.com/gocraft/work v0.5.1/go.mod h1:pc3n9Pb5FAESPPGfM0nL+7Q1xtgtRnF8rr/azzhQVlM=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
Expand All @@ -41,8 +39,6 @@ github.com/orfjackal/nanospec.go v0.0.0-20120727230329-de4694c1d701 h1:yOXfzNV7q
github.com/orfjackal/nanospec.go v0.0.0-20120727230329-de4694c1d701/go.mod h1:VtBIF1XX0c1nKkeAPk8i4aXkYopqQgfDqolHUIHPwNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
14 changes: 13 additions & 1 deletion log.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package work

import "fmt"
import (
"encoding/json"
"fmt"
)

func logError(key string, err error) {
fmt.Printf("ERROR: %s - %s\n", key, err.Error())
}

func logPanic(panic Error) {
stack, err := json.Marshal(panic.Stack)
if err != nil {
fmt.Printf("ERROR: %s - %s\n", "runJob.panic", panic.Error())
fmt.Printf("ERROR: %s - %s\n", "runJob.panic", err.Error())
}
fmt.Printf("ERROR: %s - %s\n %s - %s \n %s - %s \n", "runJob.panic", string(stack), "error message", panic.Error(), "error class", panic.Class)
}
7 changes: 3 additions & 4 deletions run.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package work

import (
"fmt"
"reflect"
)

Expand Down Expand Up @@ -42,9 +41,9 @@ func runJob(job *Job, ctxType reflect.Type, middleware []*middlewareHandler, jt
if panicErr := recover(); panicErr != nil {
// err turns out to be interface{}, of actual type "runtime.errorCString"
// Luckily, the err sprints nicely via fmt.
errorishError := fmt.Errorf("%v", panicErr)
logError("runJob.panic", errorishError)
returnError = errorishError
newErr := newError(panicErr, 2)
logPanic(newErr)
returnError = newErr
}
}()

Expand Down